Jump to page: 1 2
Thread overview
D equivalent of Python's try..else
Nov 21, 2015
Shriramana Sharma
Nov 21, 2015
rsw0x
Nov 21, 2015
Shriramana Sharma
Nov 21, 2015
rsw0x
Nov 21, 2015
Shriramana Sharma
Nov 21, 2015
Ali Çehreli
Nov 21, 2015
Russel Winder
Nov 21, 2015
Shriramana Sharma
Nov 21, 2015
Russel Winder
Nov 22, 2015
Mike Parker
Nov 22, 2015
Jonathan M Davis
Nov 22, 2015
Kagamin
Nov 22, 2015
Kagamin
Nov 22, 2015
rsw0x
November 21, 2015
Hello. In Python one has the syntax try..except..else.. where code in the else clause will only be executed if an exception does not occur. (Ref: http://stackoverflow.com/a/22579805/1503120)

In D, is there such an idiomatic/canonical construct? The D try statement only seems to support finally (apart from catch).

-- 
Shriramana Sharma, Penguin #395953
November 21, 2015
On Saturday, 21 November 2015 at 05:45:37 UTC, Shriramana Sharma wrote:
> Hello. In Python one has the syntax try..except..else.. where code in the else clause will only be executed if an exception does not occur. (Ref: http://stackoverflow.com/a/22579805/1503120)
>
> In D, is there such an idiomatic/canonical construct? The D try statement only seems to support finally (apart from catch).


scope(failure) can be used to run code when an exception is thrown inside the scope, and scope(success) only triggers if the scope exited successfully

http://ddili.org/ders/d.en/scope.html
November 21, 2015
rsw0x wrote:

> scope(failure) can be used to run code when an exception is
> thrown inside the scope, and scope(success) only triggers if the
> scope exited successfully
> 
> http://ddili.org/ders/d.en/scope.html

Thanks but I know that and it executes only at the point of scope exit. But I want some code to run immediately after the try clause but only if an exception did not occur.

The Python else clause is for code which should be run only if an exception never occurred i.e. even if one occurred and it was handled. It will be executed before `finally`. Is there a D equivalent?

-- 
Shriramana Sharma, Penguin #395953
November 21, 2015
Shriramana Sharma wrote:

> In Python one has the syntax try..except..else.. where code in the else clause will only be executed if an exception does not occur. (Ref: http://stackoverflow.com/a/22579805/1503120)

Official Python documentation: https://docs.python.org/3/reference/compound_stmts.html#try

-- 
Shriramana Sharma, Penguin #395953
November 21, 2015
On Saturday, 21 November 2015 at 05:55:53 UTC, Shriramana Sharma wrote:
> rsw0x wrote:
>
>> scope(failure) can be used to run code when an exception is
>> thrown inside the scope, and scope(success) only triggers if the
>> scope exited successfully
>> 
>> http://ddili.org/ders/d.en/scope.html
>
> Thanks but I know that and it executes only at the point of scope exit. But I want some code to run immediately after the try clause but only if an exception did not occur.
>
> The Python else clause is for code which should be run only if an exception never occurred i.e. even if one occurred and it was handled. It will be executed before `finally`. Is there a D equivalent?

Put the scope(success) inside the try block?
November 21, 2015
On 11/20/2015 09:45 PM, Shriramana Sharma wrote:
> Hello. In Python one has the syntax try..except..else.. where code in the
> else clause will only be executed if an exception does not occur. (Ref:
> http://stackoverflow.com/a/22579805/1503120)
>
> In D, is there such an idiomatic/canonical construct? The D try statement
> only seems to support finally (apart from catch).
>

I don't know what idiom that enables in Python but it feels to me like putting the statements right after the ones that could throw suffices in D (and Python):

    try {
        may_throw();
        may_throw2();
        code_for_when_they_succeed();

    } catch (/* ... */) {
        // ...
    }

Ali

November 21, 2015
On Fri, 2015-11-20 at 22:39 -0800, Ali Çehreli via Digitalmars-d-learn wrote:
> 
[…]
> I don't know what idiom that enables in Python but it feels to me
> like
> putting the statements right after the ones that could throw suffices
> in
> D (and Python):

The else clause for while, for, try, has always been a bit of an outlier bit of syntax, there as much for consistency with if as much as anything else. It has not been taken out of the language for various reasons, not least of which is that on some occasions it does make the code more comprehensible.

> 
>      try {
>          may_throw();
>          may_throw2();
>          code_for_when_they_succeed();
> 
>      } catch (/* ... */) {
>          // ...
>      }

This is entirely true but in terms of meaning:

    try:
        may_throw()
    except ...:
        ...
    else:
       code_for_when_they_succeed()

makes it clear that there is a block where exception might occur and this is what is being protected, and that the follow up code is not being protected it is there as a non-exception tail to the code. Arguable maybe, person maybe, but there are times when I really like this separation.

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.

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



November 21, 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
November 21, 2015
On Sat, 2015-11-21 at 19:26 +0530, Shriramana Sharma via Digitalmars-d- learn 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(); }
> 

There have been, over the years, many debates about changing the "else" keyword to something else, and always nothing is changed because "else" is the least worst name. In the case above, I could live with "else" or "default" since it doesn't have much conflict – but I bet this could easily become a "bikeshed issue".

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



November 22, 2015
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.
« First   ‹ Prev
1 2