Why we need scope(success) if I can write at the end?
Jonathan M Davis
jmdavisProg at gmx.com
Fri Jan 21 15:01:53 PST 2011
On Friday, January 21, 2011 14:49:44 Andrej Mitrovic wrote:
> When there are multiple calls that can fail, and where I have to do
> clean-up code in a certain order and under certain conditions I use
> scope(exit). For example:
>
> import std.stdio;
> import std.exception;
>
> void main()
> {
> foo();
> }
>
> enum NoError = true;
>
> bool Initialize() { return true; }
> bool Start() { return false; }
>
> void Terminate() { writeln("Terminated."); }
> void Stop() { writeln("Stopped."); }
>
> void foo()
> {
> auto error = Initialize();
> enforce(error == NoError, new Exception("Couldn't initialize"));
> scope(exit)
> Terminate(); // call Terminate only if Initialize was successful
>
> error = Start();
> enforce(error == NoError, new Exception("Couldn't start"));
> scope(exit)
> Stop(); // call Stop only if Start was successful
> }
>
> If I change the first scope to scope(success), Terminate will not be
> called if the call to Start() failed. This can be bad in cases where
> Terminate would release some hardware resource (it can lock up an app,
> for example). scope statements won't be ran if an exception is throwed
> before that scope statement is reached. So, that's one thing to keep
> in mind.
That's why you choose the appropriate version of scope(). And while
scope(success) is still definitely useful, it's far less useful than
scope(failure) or scope(exit).
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list