Difference between revisions of "Error"

From HaskellWiki
Jump to navigation Jump to search
(link to Error vs. Exception)
m (Clarified a bit)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
An '''error''' denotes a programming error.
 
An '''error''' denotes a programming error.
The Prelude function <hask>error</hask> represents an error with a message,
+
The Prelude function <hask>error</hask> represents an error with a custom message,
 
<hask>undefined</hask> represents an error with a standard message.
 
<hask>undefined</hask> represents an error with a standard message.
 
For a Haskell program, an <hask>undefined</hask> value cannot be distinguished from an infinite loop,
 
For a Haskell program, an <hask>undefined</hask> value cannot be distinguished from an infinite loop,
 
e.g. <hask>let x=x+1 in x :: Int</hask>. Almost not.
 
e.g. <hask>let x=x+1 in x :: Int</hask>. Almost not.
 
So ''error'' and ''[[Bottom|undefined value]]'' are somehow synonyms in Haskell.
 
So ''error'' and ''[[Bottom|undefined value]]'' are somehow synonyms in Haskell.
Since Haskell is [[non-strict semantics|non-strict]],
+
Since Haskell is [[non-strict semantics|non-strict]], the occurence of an error within an expression is not necessarily an error, if the erroneous value is ignored somewhere, e.g. <hask> False && undefined </hask>.
the occurence of an error within an expression is not necessarily an error,
 
if the erroneous value is ignored somewhere, e.g. <hask> False && undefined </hask>.
 
 
However, if an expression finally evaluates to bottom or hangs in an infinite loop, then this is definitely a programming error.
 
However, if an expression finally evaluates to bottom or hangs in an infinite loop, then this is definitely a programming error.
 
Consider for instance <hask> fromJust x </hask> or
 
Consider for instance <hask> fromJust x </hask> or

Latest revision as of 15:56, 7 August 2022

An error denotes a programming error. The Prelude function error represents an error with a custom message, undefined represents an error with a standard message. For a Haskell program, an undefined value cannot be distinguished from an infinite loop, e.g. let x=x+1 in x :: Int. Almost not. So error and undefined value are somehow synonyms in Haskell. Since Haskell is non-strict, the occurence of an error within an expression is not necessarily an error, if the erroneous value is ignored somewhere, e.g. False && undefined. However, if an expression finally evaluates to bottom or hangs in an infinite loop, then this is definitely a programming error. Consider for instance fromJust x or better fromMaybe (error "I know for sure, that x is Just, because ...") x. If the user sees according errors, then something is wrong which only the programmer can repair. These are really errors by the programmer and cannot be the final result in a correct program. Thus there is (almost) no way to recover from an error, at least no recommended one.

However, you might have programmed a server which shall also keep running, if some service crashes due to a programming error. Or you want to quit a buggy program with a message like: "The impossible happened, a program error occured. Please send an e-mail with the following stack trace to the developer." The possibility to catch such errors in the IO monad is really tempting for abuse. So please think twice before you want to catch (and hide) an error, whether the thing you want to handle is actually an exception.


See also