[Haskell-cafe] exceptions vs. Either

David Roundy droundy at abridgegame.org
Wed Aug 4 05:56:50 EDT 2004


On Tue, Aug 03, 2004 at 12:51:50PM +0200, Ketil Malde wrote:
> Is there any easy way (TH?) to amend these to output the line number
> of the offending caller?  It would be a great improvement to see
> something like
> 
>         Prelude.head : empty list in Foo.hs, line 4711
> 
> since programs generally contain many, many calls to functions like
> these.  

I include the following file in most files (and always use the C
preprocessor):

import DarcsUtils ( bug )
#define impossible (bug $ "Impossible case at "++__FILE__++":"++show (__LINE__ :: Int)++" compiled "++__TIME__++" "++__DATE__)

#define fromJust (\m -> case m of {Nothing -> bug ("fromJust error at "++__FILE__++":"++show (__LINE__ :: Int)++" compiled "++__TIME__++" "++__DATE__); Just x -> x})

Here "bug" is a function that just calls "error" with a little prefix
explaining that there is a bug in darcs, and would the user please report
it.  Obviously, defining a head here would be just as easy, but I usually
have more trouble with fromJust, which in a few places gets called in
internal routines where I *should* know that the data structure has no
Nothings, but have been known to make mistakes.

I also catch all "error" exceptions and print a nicer error message, so I
use fail and error "raw" to indicate actual user errors.
-- 
David Roundy
http://www.abridgegame.org


More information about the Haskell-Cafe mailing list