TryElseExpression DIP

pineapple via Digitalmars-d digitalmars-d at puremagic.com
Mon Sep 5 13:04:43 PDT 2016


On Monday, 5 September 2016 at 19:12:02 UTC, Jacob Carlborg wrote:
> On 2016-09-05 20:57, pineapple wrote:
>
>> In this case, the catch block will catch both errors from 
>> do_a_thing and
>> depends_on_success_of_thing.
>
> Then move it to after the "finally" block.

It would actually have to be inside the "finally" block to 
reproduce the exact behavior. I included an analog in the 
original post - I'm not saying that this behavior can't be 
achieved now, but that this is a concise and more readable 
pattern which, when utilized, makes it more difficult to 
accidentally write error-prone code.

Which is easier to read and to write? Which is more maintainable? 
Which is less prone to programmer errors? This?


     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();
         }
     }

Or this?

     try{
         do_a_thing();
     }catch(Exception exception){
         handle_error();
     }else{
         depends_on_success_of_thing();
     }finally{
         do_this_always();
     }



This?

     try{
         do_a_thing();
     }else{
         depends_on_success_of_thing();
     }

Or this?

     bool success = false;
     try{
         do_a_thing();
         success = true;
     }finally{
         if(success){
             depends_on_success_of_thing();
         }
     }


More information about the Digitalmars-d mailing list