The Right Approach to Exceptions
foobar
foo at bar.com
Tue Feb 21 08:39:26 PST 2012
On Tuesday, 21 February 2012 at 16:15:17 UTC, 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
Regarding the downcast - you still perform a check in the code
above! You gained nothing by replacing a type check with a check
on a hash.
Regarding composition of several traits - even that simple
snippet is enough:
throw new
WithRainbows!withErrorCode!withFoobar!FileNotFoundException(...);
That's without further design which could probably improve this
further.
More information about the Digitalmars-d
mailing list