DIP 1002 (TryElseExpression) added to the queue

John Colvin via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 27 23:50:34 PDT 2016


On Wednesday, 28 September 2016 at 04:23:56 UTC, Walter Bright 
wrote:
> On 9/27/2016 2:30 AM, 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
>> ```
>
> The DIP says that a state variable is currently necessary, but 
> that is incorrect:
>
> try
> {
>     scope (exit)
>     {
>         Runs unconditionally, evaluates last
>     }
>     scope (success)
>     {
>         runs when no error was raised
>     }
>     do_something();
> }
> catch (Exception e)
> {
>     Runs when an error inheriting from Exception was raised
> }


That's not the same. Exceptions thrown in the success block can 
be caught by the catch.  That scope (success) block is equivalent 
to just putting its contents immediately after do_something().



More information about the Digitalmars-d mailing list