Version 6.10.1 of GHC was released as this book went to press. It introduces an extensible extension system. In the sections that follow, we document the older exception system. The two are similar, but not completely compatible.
Exception handling is found in many programming languages,
including Haskell. It can be useful because, when a problem occurs,
exception handling can provide an easy way of handling it, even if it
occurs several layers down through a chain of function calls. With
exceptions, it’s not necessary to check the return value of every
function call for errors, and we must take care to produce a return
value that reflects the error, as C programmers must do. In Haskell,
thanks to monads and the
Maybe types, we can often achieve the
same effects in pure code without the need to use exceptions and
Some problems—especially those involving I/O—call for working with exceptions. In Haskell, exceptions may be thrown from any location in the program. However, due to the unspecified evaluation order, they can only be caught in the IO monad. Haskell exception handling doesn’t involve special syntax as it does in Python or Java. Rather, the mechanisms to catch and handle exceptions are—surprise—functions.
Control.Exception module, various functions and types relating to exceptions are defined. There is an
Exception type defined there; all exceptions are of type
Exception. There are also functions ...