why scope(success)?

Walter Bright newshound at digitalmars.com
Fri May 12 11:23:34 PDT 2006


Serg Kovrov wrote:
> 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.

Exceptions work well as long as there is only one operation that needs 
to be rolled back. As soon as you have two or more operations that 
happen sequentially, and either both must succeed or neither, that the 
traditional exception mechanism starts coming unglued.

For example, if I have operations A(), B(), and C(), and A.rollback(), 
B.rollback() and C.rollback:

	A();
	scope(failure) A.rollback();
	B();
	scope(failure) B.rollback();
	C();
	scope(failure) C.rollback();

Just for fun, try to do that with either C++ exceptions or Java try-finally.



More information about the Digitalmars-d mailing list