Error vs. Exception

From HaskellWiki
Revision as of 23:33, 5 December 2009 by Lemming (talk | contribs) (different flavors of 'catch')

Jump to: navigation, search

There is confusion about the distinction of errors and exceptions for a long time, repeated threads in Haskell-Cafe and more and more packages that handle errors and exceptions or something between. Although both terms are related and sometimes hard to distinguish, it is important to do it carefully. This is like the confusion between parallelism and concurrency.

The first problem is that exception seem to me to be the historically younger term. Before there were only errors, independent from whether they are programming or I/O or user errors. In this article we want to use the term "exception" for expected but irregular situations at runtime and the term "error" for mistakes in the running program, that can be resolved only by fixing the program.

The history may have led to the identifiers we find today in the Haskell language and standard Haskell modules.

  • Exceptions: Prelude.catch, Control.Exception.catch, Control.Exception.try, IOError, Control.Monad.Error
  • Errors: error, Control.Exception.catch, Debug.Trace.trace

Note, that the catch> function from Prelude handles exceptions exclusively, whereas its counterpart from Control.Exception also catches certain kinds of undefined values.

Prelude> catch (error "bla") (\msg -> putStrLn $ "catched " ++ show msg)
*** Exception: bla

Prelude> Control.Exception.catch (error "bla") (\msg -> putStrLn $ "catched " ++ show (msg::Control.Exception.SomeException))
catched bla

This is unsafe, since Haskell's error is just sugar for undefined, that shall help spotting a programming error. A program should work as well when all errors and undefineds are replaced by infinite loops. However infinite loops in general cannot be catched, whereas calls to sugared functions like error can.

Even more confusion was initiated by Java programming language to use the term "exceptions" for programming errors like the NullPointerException and introducing the distinction between checked and unchecked exceptions.


error handling = debugging

Examples

examples: GHC errors and exceptions

Bruker library

Modula-3 arithmetic library

When exceptions become errors

unhandled exceptions

When errors become exceptions

web servers

See also