Exception vs. Error
Sean Kelly
sean at f4.ca
Thu Mar 29 13:41:19 PDT 2007
Stewart Gordon wrote:
>
> You're probably right. I'm wondering whether it would make sense for
> Exception to derive from Error.
I don't think it would. If Errors are not recoverable, then that
suggests that Exceptions would not be recoverable as well. The reverse
problem holds when Exception derives from Error.
> Java has a class Throwable, from which Exception and Error are derived.
We considered this for Tango, but decided against it because it still
strictly defines what is and is not a recoverable error based on which
parent class is chosen. For common applications this might be fairly
straightforward, but things are less clear-cut when designing an
operating system kernel, for example. As a systems programming
language, I feel that D can't make the same assumptions that Java
typically can.
> In D, you can throw an object of any class, but having a common base
> class for exceptions and errors is still useful as it enables some of
> the workings to be shared. At the moment it's really just the exception
> message and hence toString, but who knows if it'll eventually have stuff
> like getting a stack trace?
>
> What is the 'next' member of Error for? Phobos doesn't seem to use it
> at all.
Tango has these in Exception, and they are intended for something like this:
void fnA()
{
try
{
fnB();
}
catch( Exception e )
{
throw new Exception( "Call failed with: ", e );
}
}
void fnB()
{
throw new Exception( "Danger, Will Robinson!" );
}
In essence, there are times when an enclosing routine can't entirely
recover from an exception it catches, and it may occasionally be useful
to re-categorize the exception or to provide additional information, but
also to retain the original exception for later inspection. One example
might be a low-level IO failure that causes in a cascade of failures up
the call stack. At some high level the user code might catch and handle
a LoginFailureException, but that might include a chain of:
InvalidProtocolException, InvalidSocketStateException, and
CorruptSocketBufferException.
Sean
More information about the Digitalmars-d-learn
mailing list