new syntax for cleanup (was Re: destructors order not guaranteed?)

Alex Martelli aleaxit at
Mon Nov 6 20:54:30 CET 2000

"Bjorn Pettersen" <bjorn at> writes:
> > If a function called in a C++ destructor throws, and the
> > exception exits from the destructor, what happens next is
> > pandemonium.  "A destructor must not throw" is not written
> I'm not sure I understand what you're saying here... There are two
> situations that are possible here in C++:
>  - an exception is thrown in a normal context (e.g. at the end of
>    a scope). Such an exception will be caught by the nearest 
>    enclosing handler.
>  - an exception is thrown from a destructor during stack 
>    unwinding (i.e. another exception happened). In this case
>    std::terminate() is called.
> Both cases are well defined and I would hardly call the results
> "pandemonium"...?

Sorry for being a little colorful here!-)  But, hey, you gotta
expect that from us Italians.  "Obvious" context: suppose
your destructors are doing _important_ stuff, stuff that MUST
be done -- what one would do in a "finally" clause in Java
or Python.  For example, erasing big files, intended to be
temporary, that would otherwise stay around, clog the disk,
and bring a crucial server to its knees, right?

It's not a problem that C++ does not have a 'finally' clause,
right?  As long as you take care to delegate the 'finally' stuff
to destructors of auto-storage class objects, you can rely on
that crucial finalization stuff getting executed, right...?

Wrong.  You can't, *unless* you trust *every* destructor
never to throw (and let escape) any exceptions.  If some
*other* destructor throws unappropriately, then you have
NO ensurance any more that all of your other destructors 
will get to be run, even for object in auto storage class!  This
basically makes it *impossible* to write exception-correct
code.  Including, in practice, code that will at least be
in a valid internal state unless/until terminate() is called
(hint: what semantics, if any, are ensured by destruction
of a std::vector... if other destructors can throw... even
if terminate doesn't end up getting called...?).

You must be a much more phlegmatic person than me,
if you dislike calling these results "pandemonium".


Do You Yahoo!?
Get your free address at

More information about the Python-list mailing list