473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

exception from constructor

There is a rumor around the office that it is unacceptable to throw an
exception from a constructor.
I have always thrown exceptions from constuctors. It is the only
method of handling errors!

Where in the world is this argument coming from?
Does it have any validity?

I seek knowledge o gurus of the C++
Aug 1 '08 #1
5 1538
On Jul 31, 4:59*pm, Christopher <cp...@austin.rr.comwrote:
There is a rumor around the office that it is unacceptable to throw an
exception from a constructor.
What do they recommend for when it's impossible to construct the
object?
I have always thrown exceptions from constuctors. It is the only
method of handling errors!
Of course. It is either an object or not.
Where in the world is this argument coming from?
Does it have any validity?
It comes from code that is not exception safe. In that case, any
exception can leak resources. Banning exceptions in constructors is an
attempt to get away from such problems.

Ali
Aug 1 '08 #2
Hi

Christopher wrote:
There is a rumor around the office that it is unacceptable to throw an
exception from a constructor.
I have always thrown exceptions from constuctors. It is the only
method of handling errors!

Where in the world is this argument coming from?
Does it have any validity?
To me, it sounds like they confuse constructors and destructors.
It is rarely a good idea to throw within a destructor.
http://new-brunswick.net/workshop/c+...html#faq-11.13

Markus

Aug 1 '08 #3
On Aug 1, 2:12 am, acehr...@gmail.com wrote:
On Jul 31, 4:59 pm, Christopher <cp...@austin.rr.comwrote:
There is a rumor around the office that it is unacceptable
to throw an exception from a constructor.
What do they recommend for when it's impossible to construct
the object?
An assertion failure? Constructing the object with an invalid
state, which you test before each use?

In all but a few, very rare cases, of course, exceptions are
preferable to these alternatives.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 1 '08 #4
James Kanze wrote:
No, the proper way of implementing the constructor is, as I
said, to replace the int* with std::vector< int >.
The problem is if std::vector, or any other STL container, does not do
what you want, and you are creating your own data container.

If this is the case, at some point in your code you must explicitly
write the 'new', and you have to be careful that it doesn't leak in the
case of an exception.
Aug 2 '08 #5
On Aug 2, 10:16 am, Juha Nieminen <nos...@thanks.invalidwrote:
James Kanze wrote:
No, the proper way of implementing the constructor is, as I
said, to replace the int* with std::vector< int >.
The problem is if std::vector, or any other STL container,
does not do what you want,
Then you create one that does.
and you are creating your own data container.
Then you don't have a second element whose constructor might
throw.
If this is the case, at some point in your code you must
explicitly write the 'new', and you have to be careful that it
doesn't leak in the case of an exception.
There are obviously many cases where you need an explicit new.
That doesn't change anything; whether you use a standard
container, or write some container or smart pointer or whatever
of your own. The point is that any given class can only be
responsible for one resource. The principle of RAII is that the
responsibility for a resource is always in the hands of a single
class whose sole role is to be responsible for that resource.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 2 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
11
by: Ivan A. | last post by:
Hi Is there any method to change System. Exception. Message property in derived exception's constructor? I need runtime class' information to initialize this property, but it's readonly. ...
4
by: JSheble | last post by:
If an exception occurs or is thrown in my constructor, what does the constructor actually return? A null?
8
by: JAL | last post by:
According to MSDN2, if a managed class throws an exception in the constructor, the destructor will be called. If an exception is thrown in the constructor the object never existed so how in the...
1
by: yancheng.cheok | last post by:
Hi all, According to "How can I handle a constructor that fails?" in http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2, whenever there is a constructor fail, we will throw...
3
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own...
23
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
4
by: Sunil Varma | last post by:
Hi, Here is a piece of code where the constructor throws an exception. class A { int n; public: A() try{
5
by: Vijay | last post by:
Hi All, I am not able to figure out what exactly happening in below code. what is control flow. Can anyone clear my confusion? Code: class A { public: A(){cout<<"In Constructor\n";}
11
by: Peter Jansson | last post by:
Dear newsgroup, In the following code, it looks as though the exception thrown in the constructor is not caught properly. I get this output: ---- standard output ---- Base() constructor....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.