Re: Throwing exceptions in Contstructors and Destructors?
on Sat Dec 27 2008, "Siegfried Heintze" <siegfried-AT-heintze.com> wrote:
As I recall, the only time one should not throw exceptions is
(1) inside constructors and
Constructors and exceptions are a match made in heaven because they
allow us to establish strong class invariants.
(2) destructors
Destructors and exceptions are almost always a bad idea because (among
other more conceptual reasons) destructors are invoked automatically
during stack unwinding, and if an exception is thrown during stack
unwinding the program will terminate.
because constructors and destructors can be invoked as a part of the
copy semantics.
I don't think so. Destructors are never invoked as part of a copy
operation. The only constructors invoked as part of a copy operation
are copy constructors.
When a current exception calls a destructor or constructor
Exceptions don't make calls. I think they've had their phone privileges
revoked.
that results in another exception, we have two active exception and
this ambiguous.
Anything you call can throw an exception during stack unwinding, so in
that regard constructors are not special. Destructors are only special
because they're invoked automatically and implicitly during stack
unwinding.
Well this is a problem because constructors and destructors don't have
function values thru which I can return a failure condition.
So what if I promise to never throw an instance of an object that has a
throw statement in a constructor or destructor? Would that solve the
problem?
Oh, the exception types themselves should have nothrow copy
constructors, because copy constructors may be invoked during unwinding.
--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]