Error vs. Exception
From HaskellWiki
(introduction and sketch) |
(different flavors of 'catch') |
||
| Line 11: | Line 11: | ||
The history may have led to the identifiers we find today in the Haskell language and standard Haskell modules. | The history may have led to the identifiers we find today in the Haskell language and standard Haskell modules. | ||
| - | * Exceptions: <hask>Control.Exception. | + | * Exceptions: <hask>Prelude.catch</hask>, <hask>Control.Exception.catch</hask>, <hask>Control.Exception.try</hask>, <hask>IOError</hask>, <hask>Control.Monad.Error</hask> |
| - | * Errors: <hask>error</hask>, <hask> | + | * Errors: <hask>error</hask>, <hask>Control.Exception.catch</hask>, <hask>Debug.Trace.trace</hask> |
| - | + | Note, that the <hask>catch></hask> function from <hask>Prelude</hask> handles exceptions exclusively, | |
| + | whereas its counterpart from <hask>Control.Exception</hask> also catches certain kinds of undefined values. | ||
| + | <haskell> | ||
| + | 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 | ||
| + | </haskell> | ||
| + | This is unsafe, since Haskell's <hask>error</hask> is just sugar for <hask>undefined</hask>, | ||
| + | that shall help spotting a programming error. | ||
| + | A program should work as well when all <hask>error</hask>s and <hask>undefined</hask>s | ||
| + | are replaced by infinite loops. | ||
| + | However infinite loops in general cannot be catched, whereas calls to sugared functions like <hask>error</hask> can. | ||
Even more confusion was initiated by Java programming language | Even more confusion was initiated by Java programming language | ||
| - | to use the term "exceptions" for programming errors like the <code>NullPointerException</code>. | + | to use the term "exceptions" for programming errors like the <code>NullPointerException</code> |
| + | and introducing the distinction between | ||
| + | [http://java.sun.com/docs/books/tutorial/essential/exceptions/runtime.html checked and unchecked exceptions]. | ||
Revision as of 23:33, 5 December 2009
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,IOErrorControl.Monad.Error
- Errors: ,error,Control.Exception.catchDebug.Trace.trace
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
that shall help spotting a programming error.
A program should work as well when allare replaced by infinite loops.
However infinite loops in general cannot be catched, whereas calls to sugared functions likeEven 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
Contents |
1 Examples
examples: GHC errors and exceptions
Bruker library
Modula-3 arithmetic library
2 When exceptions become errors
unhandled exceptions
3 When errors become exceptions
web servers
