On Monday, 5 September 2016 at 18:27:44 UTC, ag0aep6g wrote:
Can you point out how this is different from (and better than)

    try { do_a_thing(); depends_on_success_of_thing(); }
    catch (...) { ... }
    finally { ... }

?

On Monday, 5 September 2016 at 18:27:52 UTC, arturg wrote:
hm, isn't this similar/same as the python version?

  try{
      doSomething();

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

In this case, the catch block will catch both errors from do_a_thing and depends_on_success_of_thing. The places where this sort of pattern is most useful are where you don't want to handle errors in depends_on_success_of_thing at all, because to have thrown an error is very unexpected behavior.

Granted scenarios like that are uncommon, but the most important reason for using the separate `else` scope is to help reduce programmer error because they forgot that the error handling in the catch block will break something when it was entered because depends_on_success_of_thing failed.

There's also the matter of readability and cleanliness. It is (in my opinion, at least) an intuitive and concise way to handle a not-uncommon case.


Reply via email to