www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D equivalent of Python's try..else

reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
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).

-- 

Nov 20 2015
next sibling parent reply rsw0x <anonymous anonymous.com> writes:
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
Nov 20 2015
parent reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
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? --
Nov 20 2015
parent rsw0x <anonymous anonymous.com> writes:
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?
Nov 20 2015
prev sibling next sibling parent Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
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 --
Nov 20 2015
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Nov 20 2015
parent reply Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Fri, 2015-11-20 at 22:39 -0800, Ali =C3=87ehreli via Digitalmars-d-learn
wrote:
=20
[=E2=80=A6]
 I don't know what idiom that enables in Python but it feels to me
 like=20
 putting the statements right after the ones that could throw suffices
 in=20
 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.
=20
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0try {
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0may_throw();
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0may_throw2();
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0code_for_when_they_=
succeed();
=20
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0} catch (/* ... */) {
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0// ...
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0}
This is entirely true but in terms of meaning: try: =C2=A0 =C2=A0 may_throw() except ...: =C2=A0 =C2=A0 ... else: =C2=A0 =C2=A0code_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. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 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
Nov 21 2015
parent reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
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? --
Nov 21 2015
next sibling parent Russel Winder via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sat, 2015-11-21 at 19:26 +0530, Shriramana Sharma via Digitalmars-d-
learn wrote:
=20
[=E2=80=A6]
 Hmm =E2=80=93 I forgot Python has `else` for `for` and `while` too. But i=
t's
 a tad=20
 difficult to wrap one's mind around the meaning of the word `else` in
 this=20
 particular context whereas it actually means `nobreak`. Perhaps if
 this were=20
 added to D, `default` would be a better choice of keyword, since we
 all know=20
 that `default` (as in `switch`) is not executed if `break` happens.
=20
 So:
=20
 try { code_which_can_throw(); }
 catch { handler(); }
 default { only_if_didnt_throw(); }
 finally { mandatory(); }
=20
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 =E2=80=93 but I bet this coul= d easily become a "bikeshed issue". --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 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
Nov 21 2015
prev sibling next sibling parent reply Mike Parker <aldacron gmail.com> writes:
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.
Nov 21 2015
parent Jonathan M Davis via Digitalmars-d-learn writes:
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
Nov 22 2015
prev sibling parent reply Kagamin <spam here.lot> writes:
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`.
In a way `for` and `while` are conditional statements like `if`: when condition is true, positive branch is executed; when condition is false, `else` clause is executed.
 try { code_which_can_throw(); }
 catch { handler(); }
 default { only_if_didnt_throw(); }
 finally { mandatory(); }

 How does that look?
Both `default` and `else` match if previous clauses didn't match, there's no difference in their semantics, in `try` statement they would match if other `catch` clauses didn't match no matter if there's exception or not.
Nov 22 2015
parent reply Kagamin <spam here.lot> writes:
As an idiomatic option there can be `finally(exit)`, 
`finally(success)` and `finally(failure)` that would mirror 
semantics of scope guards.
Nov 22 2015
parent rsw0x <anonymous anonymous.com> writes:
On Sunday, 22 November 2015 at 10:01:48 UTC, Kagamin wrote:
 As an idiomatic option there can be `finally(exit)`, 
 `finally(success)` and `finally(failure)` that would mirror 
 semantics of scope guards.
how does this differ from just putting a scope(failure) inside the try block? it only triggers if no exception is thrown, else it goes to the catch.
Nov 22 2015