need to emulate scope(failure) with struct destructor

piotrklos via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 28 13:06:42 PDT 2017


I need to perform an action, in multiple separate functions, if 
scope exits with an exception. The trouble is I don't want to 
litter my code with scope(failure) everywhere. I already create 
an instance of a struct at each location, with the sole purpose 
of doing things at the end of scope.
So my code looks like:

function1()
{
RAIIType transactionHandler;
scope(failure) action;
//code
}

function2()
{
RAIIType transactionHandler;
scope(failure) action;
//code
}

function3()
{
RAIIType transactionHandler;
scope(failure) action;
//code
}

etc.

  Ideally I would put the statement from scope(failure) in the 
struct's destructor and delete the scope(failure) statements.
I would need something like c++'s std::uncaught_exceptions() to 
check if an exception is in flight.

Is there something like this in D?

PS I think that we have here a more general problem, because 
dlang is missing a feature for composition of scope(...) 
statements.



More information about the Digitalmars-d-learn mailing list