A quick-n-dirty workaround is the assert
function from Control.Exception. However, it's a little clunkier than error
.
Warning: All asserts will be silently ignored if you compile with optimizations (ghc -O1
, -O2
, etc.).
Example:
import Control.Exception
main = do
print (42 + (assert True 17)) -- adds 42 and 17
print (42 + (assert False 21)) -- crashes
Output:
59
test.hs: /tmp/test.hs:5:18-23: Assertion failed
Note the line number "5" in the output.
You could use trace
from Debug.Trace to add an error message:
print (42 + (trace "omg error" $ assert False 21))