try & catch / repeating code - DRY

Ali Çehreli acehreli at yahoo.com
Wed May 23 11:07:25 UTC 2018


On 05/23/2018 12:47 AM, Robert M. Münch wrote:
> On 2018-05-22 18:34:34 +0000, Ali ‡ehreli said:
> 
>> An idiom known in C++ circles is a Lippincott function:
>>
>>    
>> https://cppsecrets.blogspot.ca/2013/12/using-lippincott-function-for.html
>>
>> Just wanted to mention that it can be a part of a clean solution.
> 
> Thanks, and I assume that D has the same property WRT exception 
> re-throwing as C++, right?
> 

I think you have to catch and rethrow explicitly:

import std.stdio;

void main() {
     try {
         try {
             throw new Exception("Yo");
         } catch (Exception e) {
             writeln("Rethrowing");
             throw e;
         }
     } catch (Exception e) {
         writeln(e.msg);
     }
}

Rethrowing
Yo

Keeping in mind that it's possible to catch Throwable as well but it's 
considered less sanitary because it would catch Errors as well, which is 
supposed to mean "unrecoverable error". There are long discussions about 
whether one should do that or not...

Ali


More information about the Digitalmars-d-learn mailing list