The Right Approach to Exceptions

Juan Manuel Cabo juanmanuel.cabo at
Tue Feb 21 08:21:02 PST 2012

Also, you would lose the stacktrace by rethrowing with a different exception object.
(Currently, the stacktrace is lost by rethrowing the same object, but the Exception.file
and Exception.line are not lost, and it seems that it is very easy to not lose the
stacktrace when rethrowing, and it is the correct thing (for instance, java doesn't
lose the stacktrace when rethrowing, and C++ with its throw; statement for rethrowing
doesn't either).


On 02/21/2012 01:15 PM, Juan Manuel Cabo wrote:
>> FileNotFoundException is the super class of the others so the first catch clause is enough. in fact, the others will
>> never be called if listed in the above order.
> Nice! I missed that. But what if you want to add ErrorCode and Rainbows?
> And with your approach, one has to test for type and downcast, or
> otherwise have multiple catch blocks (I don't want to miss plain
> FileNotFoundExceptions). So it's square one.
> With Variant[string] (or something equivalent, nothing better comes to mind)
> one does:
>     try {
>         ...
>     } catch (FileNotFoundException ex) {
>          if (ex.hasInfo(MyNameConstant)) {
>              ... use that ...
>          }
>          ... common handling ...
>     }
> --jm

More information about the Digitalmars-d mailing list