DIP 1002 (TryElseExpression) added to the queue

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 27 03:55:47 PDT 2016


On Tuesday, 27 September 2016 at 09:30:10 UTC, Dicebot wrote:
> https://github.com/dlang/DIPs/blob/master/DIPs/DIP1002.md
>
> PR: https://github.com/dlang/DIPs/pull/43
>
> Abstract:
>
> In Python, the try/catch/finally syntax is augmented with an 
> additional clause, termed else. It is a fantastically useful 
> addition to the conventional syntax. It works like this:
>
> ```
>     try:
>         do_something()
>     except Exception as e:
>         pass # Runs when an error inheriting from Exception was 
> raised
>     else:
>         pass # Runs when no error was raised
>     finally:
>         pass # Runs unconditionally, evaluates last
> ```

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) {}
}

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) {}
}


More information about the Digitalmars-d mailing list