D equivalent of Python's try..else

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Nov 22 01:48:58 PST 2015


On Sunday, November 22, 2015 07:41:40 Mike Parker via Digitalmars-d-learn wrote:
> On Saturday, 21 November 2015 at 13:57:01 UTC, Shriramana Sharma
> wrote:
> >
> > Hmm – I forgot Python has `else` for `for` and `while` too. But
> > it's a tad difficult to wrap one's mind around the meaning of
> > the word `else` in this particular context whereas it actually
> > means `nobreak`. Perhaps if this were added to D, `default`
> > would be a better choice of keyword, since we all know that
> > `default` (as in `switch`) is not executed if `break` happens.
> >
> > So:
> >
> > try { code_which_can_throw(); }
> > catch { handler(); }
> > default { only_if_didnt_throw(); }
> > finally { mandatory(); }
> >
> > How does that look?
>
> Ugly. There's absolutely zero need for this in D.

It's trivially changed to

try { code_which_can_throw(); only_if_didnt_throw(); }
catch { handler(); }
finally { mandatory(); }

So, even assuming that scope statements aren't the best fit (e.g. because
you actually need access to the exception, or you don't want it to  be
rethrown from the catch block), the default block is pretty pointless as far
as I can tell.

- Jonathan M Davis




More information about the Digitalmars-d-learn mailing list