Exception Hierarchy [WAS: Re: Top 5]

Kirk McDonald kirklin.mcdonald at gmail.com
Thu Oct 9 14:13:06 PDT 2008


Sean Kelly wrote:
> Andrei Alexandrescu wrote:
>> downs wrote:
>>
>>> 3) the Exception/Error problem (they're different things, they
>>> shouldn't inherit!)
>>
>> I think Exception should inherit Error.
> 
> I personally dislike the use of "Error" to denote exceptions of any 
> sort.  To me, errors are what /cause/ exceptions to be thrown.  For this 
> reason, in Tango and druntime, "Exception" is defined as the top-level 
> class from which all exceptions should derive (imagine that), making it 
> equivalent to your "Error" object.
> 
> With this in mind, I'd like to solicit opinions about how exceptions 
> should be categorized.  Should unrecoverable exceptions derive directly 
> from "Exception" with a sibling named something like 
> "RecoverableException" as the parent for recoverable exceptions?  Or 
> would a bit more structure be better?  I don't think it should be the 
> runtime's responsibility to define a complex exception hierarchy, but 
> there is clearly a desire to at least retain some distinction between 
> recoverable and unrecoverable errors and my naming-fu is not terribly 
> strong.
> 
> 
> Sean

I suggest looking at Python's exception hierarchy for inspiration:

http://docs.python.org/library/exceptions.html

The root of its exception tree is the BaseException type. The regular 
Exception type (whence all user exceptions should be derived) is a 
subclass of it. The purpose of this arrangement is so certain exceptions 
that should not be caught by an "except Exception:" block will get 
through. Exceptions that inherit from BaseException directly include 
SystemExit (the exception thrown to exit the program) and 
KeyboardInterrupt (thrown when the user presses ctrl-C at the console).

This arrangement works well. It clearly delineates the "userland" 
exceptions (those which inherit from Exception which everyday code can 
interact with regularly) and those which are considered part of the 
system, which should not be touched (unless you really want to).

-- 
Kirk McDonald



More information about the Digitalmars-d mailing list