scope + destructor with Exception parameter for RAII

Sean Kelly sean at f4.ca
Tue Nov 28 12:18:55 PST 2006


Leandro Lucarella wrote:
> What do you think of adding an optional parameter (exception) to the 
> destructor, defaulting to null, to indicate the destructor was called 
> then unwinding because an exception was thrown? Then you can almost 
> forget about scope(exit/success/failure) and you have a RAII as complete 
>  as Python's 'with' statement.

I don't like it, personally.  It doesn't seem a good idea for a dtor to 
alter its behavior based on whether an exception is in flight, and 
exceptions should never be thrown from dtors anyway.  Doing so makes 
writing correct code far too complicated.

One thing I have done for Ares, however, is to terminate the program if 
one exception is thrown while another is in flight.  I think DMD/Phobos 
does not do this currently, and instead either ignores the new 
exception, or substitutes it for the in-flight exception (I can't 
remember which).


Sean



More information about the Digitalmars-d mailing list