[Haskell-cafe] exceptions vs. Either
alastair at reid-consulting-uk.ltd.uk
Tue Aug 3 08:21:50 EDT 2004
> Prelude.head : empty list
> Prelude.read : no parse
> and Prelude.(!!) : index too large
> and so on.
> Is there any easy way (TH?) to amend these to output the line number
> of the offending caller?
In a program of any size, I usually avoid using these functions and instead
define functions like:
--| 1st arg is error message for empty lists
head' :: Doc -> [a] -> a
--| 1st arg is description of kind of thing in list
-- Reports error if length of list /= 1
unique :: Doc -> [a] -> a
Another approach is to use a function:
inContext :: String -> a -> a
(implemented using mapException) like this:
inContext "evaluating expression" (eval env e)
to transform an exception of the form:
error "Division by zero"
error "Division by zero while evaluating expression"
Since the inContext function is rather like ghc's profiling function 'scc', it
would be nice if ghc had a command-line flag to insert a call to inContext
everywhere that it inserts a call to scc when you use -prof-all. (Of course,
you'd probably take a big stack-space hit from doing so since the chain of
calls to inContext is equivalent to the stack you would expect if using call
by value without tail call optimization.
More information about the Haskell-Cafe