The Right Approach to Exceptions

deadalnix deadalnix at gmail.com
Tue Feb 21 14:47:36 PST 2012


Le 21/02/2012 22:15, Andrei Alexandrescu a écrit :
>> What if instead of catching by class, we catch by attribute matching?
>> So instead of writing:
>>
>> try { ... }
>> catch(SomeExceptionType e) { ... }
>> catch(SomeOtherExceptionType e) { ... }
>> catch(YetAnotherSillyException e) { ... }
>>
>> we write:
>>
>> try { ... }
>> catch(e: exists!e.filename&& e.failedOp is File.open) {
>> // something
>> }
>> catch(e: e.is_transient&& e.num_retries< 5) {
>> // something else
>> }
>> // And why should we even need an exception object in the first
>> // place?
>> catch(time()>= oldtime+5000) {
>> // This thing's been running for way too long, time to
>> // do something drastic
>> }
>>
>> Flamesuit on! ;-)
>
> The only problem I see here is ascribing e a type.
>

That is why I proposed here : 
http://forum.dlang.org/thread/jhos0l$102t$1@digitalmars.com?page=25#post-jhtus4:2416tp:241:40digitalmars.com 
some alternative syntax. To make it short :

try {
     // Stuff . . .
} catch(Exception e) if(e.transient == false) {
     // Handling . . .
}

For the longer explanation, see the link.


More information about the Digitalmars-d mailing list