The Right Approach to Exceptions

foobar foo at bar.com
Tue Feb 21 08:09:49 PST 2012


On Tuesday, 21 February 2012 at 15:38:15 UTC, Juan Manuel Cabo 
wrote:
>> This works:
>> // note: the int parameter above isn't static
>> dbConn.query("select age from people where id='foobar'");
>> throw new WithErrorCode!FileNotFoundException(
>>           db.rs.getValue(1), "file not found");
> ...
>> Can you offer a real world use-case where the above isn't 
>> sufficient?
>
>
> What happened is that a file wasn't found. What one wants to 
> catch is
> a FileNotFoundException.
>
> Do you suggest that I have to:
>
>    try {
>        ...
>    } catch (FileNotFoundException ex) {
>        ...
>    } catch (WithErrorCode!FileNotFoundException ex) {
>        ...
>    } catch (WithRainbows!FileNotFoundException ex) {
>        ...
>    }
> and so on?
>
> --jm

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.


More information about the Digitalmars-d mailing list