The Right Approach to Exceptions

Jonathan M Davis jmdavisProg at gmx.com
Sat Feb 18 16:28:25 PST 2012


On Saturday, February 18, 2012 17:53:52 Andrei Alexandrescu wrote:
> On 2/18/12 5:47 PM, Jakob Ovrum wrote:
> > you are basically arguing against exceptions here
> 
> I must have argued my question and point very poorly.

You definitely seem to be arguing for a single Exception type, which does _not_ 
make for good error handling code, since all it tells you is that _something_ 
went wrong, not what. And that goes against how Exceptions are designed. They 
purposely are designed to have an inheritance hierarchy.

I grant you that simply having an exception type per module which is identical 
to Exception save for its name and the fact that it's derived from Exception 
rather than Throwable is not the best way to go. I do think that it's somewhat 
better than just using Exception everywhere, since it gives you a better idea 
of went wrong (e.g. UTFException rather than a TimeException), but ideally, it 
would give much better information than that, and we'd have a better exception 
hierarchy with related exceptions being derived from one another or from a 
common exception type rather than simply being based on what module they're 
in.

C++ is a horrible example of how exceptions should be done, so if you're 
basing what you want off of that, then that makes me think that you should be 
better familiar with how other, more recent languages use them (though maybe 
you're quite familiar with how C# and/or Java use Exceptions, I don't know). 
>From using Java, I think that how it handles exceptions in general is _far_ 
superior to how they're frequently dealt with in C++ (though that does tend to 
depend on who's doing the developing, since you _can_ have a decent exception 
hierarchy in C++).

- Jonathan M Davis


More information about the Digitalmars-d mailing list