Exceptions in constructor?

chip@atlantic.net chip@atlantic.net
Mon Oct 27 12:10:00 GMT 1997


According to ernst.molitor@uni-bonn.de:
> :According to Paul Koning:
> :> >>>>> "chip" == chip  <chip@atlantic.net> writes:
> :>  chip> I'm 99.44% pleased with the exception-based code I can now
> :>  chip> write and test under Linux on x86.  The remaining .56% would be
> :>  chip> fulfilled if throwing an exception in a constructor
> :>  chip> automatically called the appropriate operator delete () if
> :>  chip> there had been a call to operator new ().
> :>
> :> Does the C++ standard say to do that?  It would surprise me.
> :
> :Of course!  Without it, exceptions thrown in (or through) constructors
> :would cause irreparable memory leaks.
> 
> I don't have the standard at hands, but B. Stroustrup, in his book
> "C++ Programming Language" (3rd ed. paperback), states that a
> destructor will be called only for objects that are "completely
> constructed" (p. 367).  Depending on the source code in question, this
> may mean that an exception thrown in a constructor will _not_ cause a
> call of the destructor, where the call to delete would be expected...

A delete operator can be called without calling the destructor.  That
is, in fact, what the draft ANSI standard requires, when a constructor
of an object created with 'new Class' throws an exception.
-- 
Chip Salzenberg          - a.k.a. -           <chip@pobox.com>
        "He's Mr. Big of 'Big And Tall' fame."  // MST3K



More information about the Gcc mailing list