DIP 1002 (TryElseExpression) added to the queue

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 27 06:22:53 PDT 2016


On 9/27/16 6:55 AM, John Colvin wrote:
>
> What's annoying is that we already have finally,  which can be
> reproduced with other language features:
>
> {
>     /*finally*/ scope (exit) {}
>     try {
>         do_something();
>     } catch (Exception e) {}
> }

Hm... I always thought scope(exit) is lowered to:

try
{
}
finally
{
    // scope exit code here
}

Which one is the building block? ;)

> but we don't (yet) have catch else, which is harder to immitate. Options
> are:
> A) the else clause is nothrow (in which case it can just go last inside
> the try)
> or
> B) store a flag and have to use immitation finally:
>
> {
>     /*finally*/ scope (exit) {}
>     bool exceptionThrown = false;
>     try {
>         doSomething();
>     } catch (Exception e) {
>         exceptionThrown = true;
>     }
>     /*else*/ if (!exceptionThrown) {}
> }

I tried a few things, including scope(success), but it doesn't seem 
doable without an extra piece of data.

Essentially, the else clause allows you to split your try block into 
catch-protected code, and non-catch-protected code.

Seems like a worthwhile addition, if easily implemented.

-Steve


More information about the Digitalmars-d mailing list