TryElseExpression DIP

arturg via Digitalmars-d digitalmars-d at puremagic.com
Mon Sep 5 11:27:52 PDT 2016


On Monday, 5 September 2016 at 18:07:52 UTC, pineapple wrote:

> 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
>
> Imitating this functionality in D,
>
>     try{
>         do_a_thing();
>     }catch(Exception exception){
>         handle_error();
>     }else{
>         depends_on_success_of_thing();
>     }finally{
>         do_this_always();
>     }
>
> Would be equivalent to
>
>     bool success = false;
>     try{
>         do_a_thing();
>         success = true;
>     }catch(Exception exception){
>         handle_error();
>     }finally{
>         try{
>             if(success){
>                 depends_on_success_of_thing();
>             }
>         }finally{
>             do_this_always();
>         }
>     }

hm, isn't this similar/same as the python version?

   try{
       doSomething();

       scope(success) "if no Exception thrown".writeln;
   }
   catch(Exception e)
   {
        ...
   }



More information about the Digitalmars-d mailing list