Exception vs. Error

Sean Kelly sean at f4.ca
Mon Mar 26 13:24:53 PDT 2007


Lionello Lunesu wrote:
> What's the difference between an Exception and an Error?
> 
> I know that Exceptions are meant to be recoverable and Errors are 
> non-recoverable, but this doesn't help me since
> (1) Error is derived from Exception (so any catch(Exception) can 
> _recover_ from an Error), and
> (2) there are also many catch(Error) which seems that an Error is not 
> quite that bad.

Personally, I don't see any point in having the Error class, for the 
reasons mentioned above.  The only useful thing about Error is that it 
contains file and line info, and errors can be chained.  In Tango, these 
features have been added to Exception and Error has been dropped.

> Take Phobos for example. Why are some problems throwing an Error and 
> others an Exception?

Your guess is as good as mine.

> In either case, it seems like the Error class should not derive from the 
> Exception class, since catching one should not consume the other.

Right.  And then there's the issue of what is actually recoverable. 
Personally, I think this varies based on the type of application being 
written.  As a result, I don't think the language should dictate that 
some errors are recoverable and other ones are not.  Rather, the 
exception hierarchy should be designed so that errors which are 
typically not recoverable can not be easily ignored, and the rest is up 
to the user.  I tried this in Tango by making all such errors direct 
descendants of Exception (because everything thrown in D is supposed to 
derive from Exception) and suggesting rather strongly that "catch 
Exception" is generally an error and should never be done by the average 
user.

Sean


More information about the Digitalmars-d-learn mailing list