Why we need scope(success) if I can write at the end?

Andrej Mitrovic andrej.mitrovich at gmail.com
Fri Jan 21 14:49:44 PST 2011


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.


More information about the Digitalmars-d-learn mailing list