D equivalent of Python's try..else

Shriramana Sharma via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 21 05:56:57 PST 2015


Russel Winder via Digitalmars-d-learn wrote:

> else on for and while, whilst technically redundant as well, does
> occasionally make for a nicer read, for very analogous reasons. It can
> generally avoid the need for extra booleans and other state variables.

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?

-- 
Shriramana Sharma, Penguin #395953


More information about the Digitalmars-d-learn mailing list