The Right Approach to Exceptions

Ben Davis entheh at cantab.net
Sun Feb 19 04:48:23 PST 2012


On 19/02/2012 01:54, Jonathan M Davis wrote:
> On Sunday, February 19, 2012 01:43:27 Ben Davis wrote:
>> - distinguish between 'bug' exceptions (e.g. null) and 'you're more
>> likely to want to catch this' exceptions (e.g. IO). Maybe the bug ones
>> should have been Errors, since people *usually* don't catch those (and
>> are certainly never required to). As it stands, Errors are reserved for
>> corrupt class files and other VM panic scenarios.
>
> Well, in D, you'd use exceptions derived from Error (though null pointers
> still result in a good ol' segfault). Java's situation is marred by the whole
> thing with checked exceptions. D's basic is better in that regard, I think.
> It's the lack of a proper exception hierarchy where we're worse.

Indeed, I'd noticed I was getting RangeErrors, not RangeExceptions :)

>> - make sure the 'lazy' approach is a good one: the less you type, the
>> fewer exceptions you catch, and the less likely you are to silence those
>> exceptions at runtime. Probably the main problem with the exception
>> hierarchy is that the short names tend to be the more general ones that
>> are more dangerous to catch indiscriminately.
>
> You mean, make the standard exception names really long and ugly, and the more
> specific ones short? e.g. Exception becomes something like
> StandardExceptionThatYouReallyShouldBeCatchingCatchTheDerivedType whereas
> specific exceptions are more like SpecifcEx (though that's a bit extreme)?
>
> Well, while that's a good sentiment, the very nature of more specific
> exceptions means that their namesj are likely to be more specific and therefore
> longer (especially when you have descriptive names). So, I'm not sure that
> that's ultimately all that reasonable, even if the basic idea is a good one.

LOL - yeah, there's a reason I didn't propose any actual names. It's not 
an easy problem to solve. But I felt the psychology angle might not have 
been covered and was worth mentioning all the same.

You could name the class "AllExceptions" though.

But as the insidious ones like off-by-ones are Errors (or segfaults), 
it's definitely a much smaller problem in D than in Java.


More information about the Digitalmars-d mailing list