DIP 1002 (TryElseExpression) added to the queue

Nick Treleaven via Digitalmars-d digitalmars-d at puremagic.com
Fri Sep 30 09:11:21 PDT 2016


On Tuesday, 27 September 2016 at 10:55:47 UTC, John Colvin wrote:
> 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) {}
> }

C) Use collectException:

import std.exception;
auto ex = collectException!MyException(doSomething());
if (ex) handleException(ex);
else {
     // doSomething succeeded
     ...
}

collectException could be improved to catch more than one type of 
exception, returning the common type. Alternatively it could 
maybe return a tuple of the exception and the 1-based index of 
which type of exception was thrown.


More information about the Digitalmars-d mailing list