The Right Approach to Exceptions

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

> 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 ...


More information about the Digitalmars-d mailing list