Topic: constructors and exceptions
Author: rgp@mpd.tandem.com (Ramon Pantin)
Date: 21 Aug 92 03:00:24 GMT Raw View
Could one of the ANSI-C++ folks that have "paper experience" :-)
with how exception handling is supposed to work give a summary of how
will an exception thrown from a constructor interact with objects
previously constructed in the same "try" block.
Also would you please illuminate us on how an exception thrown from
the constructor of a derived object (which executes after the construction
of the base object) interacts with the destruction of the base object.
Will the destructor for the base object be called when the exception
is thrown?
What about an exception thrown from a signal handler (yuck!) that
interrupted the construction of an object?
Ramon Pantin
Author: mendell@turing.toronto.edu (Mark Mendell)
Date: 21 Aug 92 03:25:04 GMT Raw View
Real working exception handling in the IBM xlC C++ compiler on the RS/6000:
In article <2286@devnull.mpd.tandem.com> you write:
>
>Could one of the ANSI-C++ folks that have "paper experience" :-)
>with how exception handling is supposed to work give a summary of how
>will an exception thrown from a constructor interact with objects
>previously constructed in the same "try" block.
An exception in a constructor will destroy the object being constructed
(as far as it has been constructed), then destroy all previously constructed
objects in the try block, and then try matching the catch clauses. This will
repeat up the static and dynamic blocks until a catch block is found that
matches the exception, or else terminate() will be called.
>
>Also would you please illuminate us on how an exception thrown from
>the constructor of a derived object (which executes after the construction
>of the base object) interacts with the destruction of the base object.
>Will the destructor for the base object be called when the exception
>is thrown?
Any part of the object that has been created will be destroyed (in reverse
order). In particular the base destructor will be called, but not the
derived destructor, because the derived object isn't completely contructed
yet.
>
>What about an exception thrown from a signal handler (yuck!) that
>interrupted the construction of an object?
Under xlC, you are not allowed to do a throw from a signal handler. This
restriction is due to the implementation of exception handling. Different
implementations might allow you to do this. On the other hand, the only
portable action allowed by ANSI C in a signal handler is to set a variable
of type sigatomic_t.
--
Mark Mendell
IBM Toronto Lab
mendell@torolab6.vnet.ibm.com