Negative

Don Clugston dac at nospam.com.au
Wed Mar 1 03:39:07 PST 2006


Walter Bright wrote:
> I hope that these buggy examples show just how hard it is to get 
> try-catch-finally to be correct, and how easy it is to get the on_scope 
> correct. This leads me to believe that try-catch-finally is just 
> conceptually wrong, as it does not match up with how we think despite being 
> in common use for over a decade. 

This is very interesting. Seems to me that these on_scope_xxx are kind 
of "naked destructors". If you consider them to be the low-level 
construct, then putting a destructor into a class means: when the 
constructor is called, insert "on_scope_exit ~this()" at the same time.

Is the convoluted nature of the RAII solution simply because
the same destructor is executed regardless of whether the function was 
exited normally, or whether an exception occurred? And because there's 
no easy way of determining if you are inside an exception handler. So 
you only have "finally", without "catch".
I wonder if destructors could be jazzed up, so that they can insert an 
"on_scope_failure" as well as "on_scope_exit"?
Maybe called ~catch()?

class Foo
{
~catch() {
unwind_foo();
}
}

class Bar
{
   ~this() { // destroy bar
    }
   ~catch() {
    unwind_bar();
   //  now we go to ~this(), which behaves like finally.
   }
}

Transaction abc()
{
     Foo f;
     Bar b;
     Def d;

     f = dofoo();
     b = dobar();
     d = dodef();

     return Transaction(f, b, d);
}

Doesn't have the flexibility of on_scope, where you have access to all 
variables -- but on the other hand, it has the RAII benefit that users 
of the class don't need to remember to do it.

Just an idea.



More information about the Digitalmars-d mailing list