Revision as of 14:53, 25 September 2008

An exception denotes an unpredictable situation at runtime, like "out of disk storage", "read protected file", "user removed disk while reading", "syntax error in user input".
These are situation which occur relatively seldom and thus their immediate handling would clutter the code which should describe the regular processing.
Since exceptions must be expected at runtime there are also mechanisms for (selectively) handling them.

In general you should be very careful, not to mix up exceptions with errors.
Actually, an unhandled exception is an error.

1 Implementation

The great thing about Haskell is, that it is not necessary to hard-wire the exception handling into the language.
Everything is already there to implement definition and handling of exceptions nicely.

See the implementation in

Control.Monad.Error

(and please, excuse the misleading name, for now).

There is an old dispute between C++ programmers on whether exceptions or error return codes are the right way.
Also Niklaus Wirth considered exceptions to be the reincarnation of GOTO and thus omitted them in his languages.
Now Haskell solves the problem the diplomatic way:
Function return error codes, but the handling of error codes does not uglify the calling code.

First we implement exception handling for non-monadic functions.
Since no IO functions are involved, we can still not handle exceptional situations induced from outside the world,
but we can handle situations, where it is unacceptable for the caller to check a priori whether the call can succeed.