Re: is such exception handling approach good?

"Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx> wrote in message

"ajk" <ak@xxxxxxxxxxxx> wrote in message
On Fri, 21 Dec 2007 10:46:40 -0500, "Larry Smith"
<no_spam@xxxxxxxxxxx> wrote:

it is normally not a good idea to do operations in the ctor that could
cause an exception. the reason for this is that if an exception is
ever thrown in your ctor you end up with an invalid object. therefore
it is better to have some other method to initialize such with and
handle a failure outside the ctor like bool init() or something.

That's completely false as Alex stated.

i forgot to add: the dtor is never called when ctor throws an
exception so the heck do you do the cleanup if the ctor has allocated
three different things?

constructor function-try block

Sorry, it can't refer to members

The member have to clean themselves up by defining appropriate destructors.

please tell me since you obviously have such insight.


Relevant Pages

  • Re: Exception handling?
    ... Maybe with FP, exception trapping could now be considered even more important because in classic FP, its not something you think about, you can't if you wish to stack commands which is also the same idea with OOPS based protocol stacking. ... accept "If this function returns TRUE, then the CString & contains the result, but if this ... But I never really understood why a FP approach was not a big consideration for construction: ... With two-step construction you don't need to throw exception in ctor, because the default ctor just puts the object in a safe state and the actual construction is done in a /ad hoc/ construction method, Create...). ...
  • Re: is such exception handling approach good?
    ... same time, the construction of m_Whatever is successful, but the construction ... of current instance is failed and exception is thrown -- throw ... if the caller of the constructor MyClass() is smart enough, ... handle a failure outside the ctor like bool initor something. ...
  • Re: is such exception handling approach good?
    ... cause an exception. ... ever thrown in your ctor you end up with an invalid ... There is nothing wrong with throwing from constructor. ... won't end up with invalid object. ...
  • Re: Exception-safe constructors
    ... > for the field a and b in class bidon should be called after the ... > exception is thrown in the constructor; ...
  • Re: CArray
    ... About the copy ctor: I used to think that, if no copy ctor is explicitly ... the compiler provides a default one which does member-wise copy. ... Even if I wonder about the exception system... ... So, if your STL container class throws an exception, this exception is not ...