try..catch..pass..finally Was: DMD 0.148 - scope guard
Walter Bright
newshound at digitalmars.com
Wed Mar 1 02:15:35 PST 2006
"S. Chancellor" <dnewsgr at mephit.kicks-ass.org> wrote in message
news:du376e$1fbf$2 at digitaldaemon.com...
> Something like this would be equivalent?
>
> Transaction abc()
> {
> Foo f;
> Bar b;
> Def d;
> Auto scoped = new Scoper(); //scoped.exit() can be called by the
> destructor. We won't add a finally block.
>
> try {
> scoped.failures ~= void delegate () { dofoo_unwind(f); }
> f = dofoo();
>
> scoped.failures ~= void delegate () { dobar_unwind(b); }
> b = dobar();
>
> scoped.success()
> } catch (Exception o) {
> scoped.failed()
> throw o;
> }
>
> return Transaction(f, b, d);
> }
Should be:
f = dofoo();
scoped.failures ~= void delegate () { dofoo_unwind(f); }
etc. Other than that, it looks like it'll work, but it's a lot more code
than on_scope. You also need to be sure that the delegates don't refer to
any variables declared inside the try block, as those variables no longer
exist in the catch block - and the compiler can't catch that error. This
isn't a problem with on_scope.
More information about the Digitalmars-d
mailing list