The Right Approach to Exceptions

foobar foo at bar.com
Tue Feb 21 02:48:52 PST 2012


On Tuesday, 21 February 2012 at 02:23:58 UTC, Andrei Alexandrescu 
wrote:
> On 2/20/12 7:02 PM, Juan Manuel Cabo wrote:
>> oops, sorry!! I just saw a post by someone named Jose. My 
>> thousand apollogies!!
>
> I got confused. It was your argument I meant to refer to - 
> adding info to the exception in flight.
>
> Andrei

I'd implement this along these lines:

class WithErrorCode(E) : E {
   int errorCode;
   this(Args)(int err, Args args) { this.errorCode = err; 
super(args); }
}

and add this wrapper where relevant. e.g. replace:
throw new FileNotFoundException(...);
with something like:
throw new WithErrorCode!FileNotFoundException(-1, ...);

This is a localized change that doesn't affect all uses of 
exceptions and it remains type-safe. surely this is a better 
solution than the hash table?


More information about the Digitalmars-d mailing list