scope + destructor with Exception parameter for RAII

Leandro Lucarella llucarella at integratech.com.ar
Wed Nov 29 05:52:33 PST 2006


Sean Kelly escribió:
> 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.

Exceptions would not be thrown from the dtor, it just executes some code 
if it was executed during an unwinding because an exception was raised:

scope Transaction t = new Transaction();
throw Exception("ouch!");

Here the Transaction.~this(Exception) is called, but there is no 
Exception throwing in the dtor.

I don't see why writing correct code is that complicated. And how do you 
address the problem of repeating error handling code and the lack of 
encapsulation of scope(success/failure)?

Any other thoughts on this (the thread diverged a little from the 
original topic ;)?

-- 
Leandro Lucarella
Integratech S.A.
4571-5252



More information about the Digitalmars-d mailing list