why scope(success)?

Serg Kovrov dyh at pathlink.com
Wed May 10 02:42:01 PDT 2006


I used to manage it with exceptions, and more or less happy with it:
> deleteNode(node_id)
> {
>   node = getNode(node_id);
>   if (!node || !db.hasSQLFeature(SQL_TRANSACTIONS))
>   {
>     return false;
>   }
> 
>   db.beginTransaction();
>   try
>   {
>     ...
>     lots of stuf that may throw,
>     or maybe throw something right here...
>     ...
>   }
>   catch (Exception e)
>   {
>     db.rollbackTransaction();
>     return false;
>   }
> 
>   db.commitTransaction();
>   return true;
> }

In this particular case I don't want rollback on returning false, just 
if a db operation failed. And exceptions handle that rather well.

My point is, if case is a bit more complicated then a 'hello world' 
example, there most likely will be different error conditions, and they 
meant be handled differently. Exceptions are convenient way to do it.

IMHO 'on scope exit' approach is not much different from 'one return at 
end of function' with condition flag(s).

And it is kind of obscure code for me - it can be anywhere in function, 
maybe even may times, etc... It's hard to maintain (just like goto's), 
and as so, it's no use for me at this point.

Sean Kelly wrote:
> Ben Hinkle wrote:
>> I hope this doesn't come of as a flame, but I'm wondering if anyone is 
>> using scope(success) and why. I can't find any reason for it.
> 
> How about a DB transaction you only want to commit if there are no 
> errors generating data.  You might want something like this:
> 
>     scope(success) tran.commit();
>     scope(failure) tran.rollback();
> 
> Sean



More information about the Digitalmars-d mailing list