try & catch / repeating code - DRY

Robert M. Münch robert.muench at saphirion.com
Thu May 24 06:05:18 UTC 2018


On 2018-05-22 18:33:06 +0000, Jacob Carlborg said:

> You can always create a function that takes a delegate or lambda and 
> handles the exception in the function. Here are three versions of the 
> same thing, depending on how you want the call site to look like.

Hi, great! Thanks for the examples... BTW: Is there a place where such 
generic and fundamental examples are collected?

> void handleException1(alias dg)()
> {
>      try dg();
>      catch (Exception e) { /* handle exception */ }
> }
> 
> void handleException2(lazy void dg)
> {
>      try dg();
>      catch (Exception e) { /* handle exception */ }
> }
> 
> void handleException3(scope void delegate () dg)
> {
>      try dg();
>      catch (Exception e) { /* handle exception */ }
> }
> 
> void main()
> {
>      handleException1!({
>          writeln("asd");
>      });
> 
>      handleException1!(() => writeln("asd"));
> 
>      handleException2(writeln("asd"));
> 
>      handleException3({
>          writeln("asd");
>      });
> }

What is exactly the difference between handleException1 and 3?

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



More information about the Digitalmars-d-learn mailing list