scope + destructor with Exception parameter for RAII

Pragma ericanderton at yahoo.removeme.com
Tue Nov 28 13:56:23 PST 2006


BCS wrote:
> Sean Kelly wrote:
>> 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
> 
> 
> I think this works under DMD
> 
> try
> {
>     throw new Error("some stuff");
> }
> catch(Error e)
> {
>     throw new Error("more stuff\n"~e.toString);
> }
> 
> 
> It could be vary handy to have this ability to swap out one exception 
> for another.

Agreed.  Although I don't know if this interferes with Sean's 
interpretation of the spec or not.  I've done this in a few places 
myself, and I find it quite useful.

-- 
- EricAnderton at yahoo



More information about the Digitalmars-d mailing list