www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - scope(faliure) flow control.

reply "TheFlyingFiddle" <kurtyan student.chalmers.se> writes:
int foo()
{
    scope(failure) return 22;
    throw new Exception("E");
}

unittest
{
    assert(foo() == 22);
}

Is this defined behavior? At least in x64 dmd the exception is 
swallowed and the assert evaluates to true.

In any case what should happen? Should the method return or 
should the exception be propagated up the callstack?
Dec 28 2013
parent reply "Casper =?UTF-8?B?RsOmcmdlbWFuZCI=?= <shorttail hotmail.com> writes:
On Saturday, 28 December 2013 at 20:31:14 UTC, TheFlyingFiddle 
wrote:
 int foo()
 {
    scope(failure) return 22;
    throw new Exception("E");
 }

 unittest
 {
    assert(foo() == 22);
 }

 Is this defined behavior? At least in x64 dmd the exception is 
 swallowed and the assert evaluates to true.

 In any case what should happen? Should the method return or 
 should the exception be propagated up the callstack?
It's rewritten as follows: int foo() { try { throw new Exception("E"); } catch (Exception e) { // or whatever the D syntax is, I never used it return 22; } } So yes, it's intended. scope(exit) uses finally instead of catch.
Dec 28 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 12/28/2013 12:50 PM, "Casper Færgemand" <shorttail hotmail.com>" wrote:

 On Saturday, 28 December 2013 at 20:31:14 UTC, TheFlyingFiddle wrote:
 int foo()
 {
    scope(failure) return 22;
    throw new Exception("E");
 }

 unittest
 {
    assert(foo() == 22);
 }

 Is this defined behavior? At least in x64 dmd the exception is
 swallowed and the assert evaluates to true.

 In any case what should happen? Should the method return or should the
 exception be propagated up the callstack?
It's rewritten as follows: int foo() { try { throw new Exception("E"); } catch (Exception e) { // or whatever the D syntax is, I never
used it
        return 22;
There must also be the re-throwing of the caught exception: throw e; What happens is, the return statement does not allow that to happen.
     }
 }

 So yes, it's intended. scope(exit) uses finally instead of catch.
The spec brings restrictions to scope(exit) and scope(success) but does not say much about scope(failure): http://dlang.org/statement.html#ScopeGuardStatement Yeah, it appears that OP's code is legal. Ali
Dec 28 2013