D equivalent of Python's try..else

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 20 22:39:00 PST 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



More information about the Digitalmars-d-learn mailing list