The Right Approach to Exceptions

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Feb 21 06:13:53 PST 2012


On 2/21/12 4:48 AM, foobar wrote:
> 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?

The two approaches don't compete as one is static and the other is 
dynamic. For example, how does one add contextual information "While 
opening table in {database}" to the current exception of type 
WithErrorCode!FileNotFoundException?


Andrei


More information about the Digitalmars-d mailing list