DIP 1002 (TryElseExpression) added to the queue

pineapple via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 27 03:06:26 PDT 2016


On Tuesday, 27 September 2016 at 09:48:42 UTC, Jonathan M Davis 
wrote:
> And why not just put the code that would go in the else at the 
> end of the try block? Just like with this proposed else, the 
> code would only run if the preceding code didn't throw any 
> exceptions. This just seems like an attempt to make D more like 
> python rather than to add anything useful.
>
> - Jonathan M Davis

This is a commonly-used tool that makes code more readable and 
cuts down on programmer error.

It's far more concise and digestible than the functional 
equivalent, which is _not_ to put the code at the end of the 
`try` block, it's to put it in the `finally` block - refer to the 
DIP's example for how that works.

This is the most important difference between using `else` and 
doing what you described: Exceptions thrown by the code in the 
`else` block are not caught by the `catch` statements intended to 
handle errors by the operation in the `try` block, but not to 
handle errors resulting from attempting to handle a success state.


More information about the Digitalmars-d mailing list