I don't think it is a bug, but I am a bit puzzled as to why that doesn't work. A bonus question is why does it mention variable e? There is no variable e.
Prelude> :m +Control.Exception Prelude Control.Exception> handle (\_-> return "err") undefined <interactive>:1:0: Ambiguous type variable `e' in the constraint: `Exception e' arising from a use of `handle' at <interactive>:1:0-35 Probable fix: add a type signature that fixes these type variable(s) Prelude Control.Exception>
Apparently it works fine in ghci 6.8, I am using 6.10.1.
Edit: I have minimized the code. I expect that to have the same result in both 6.8 and 6.10
class C a
foo :: C a => (a -> Int)-> Int
foo _ = 1
arg :: C a => a -> Int
arg _ = 2
bar :: Int
bar = foo arg
trying to compile it:
[1 of 1] Compiling Main ( /tmp/foo.hs, interpreted ) /tmp/foo.hs:12:10: Ambiguous type variable `a' in the constraint: `C a' arising from a use of `arg' at /tmp/foo.hs:12:10-12 Probable fix: add a type signature that fixes these type variable(s) Failed, modules loaded: none. Prelude Control.Exception>
Best Answer
The type of
Control.Exception.handle
is:The problem you are seeing is that the lambda expression
(\_ -> return "err")
is not of typee -> IO a
wheree
is an instance ofException
. Clear as mud? Good. Now I'll provide a solution which should actually be useful :)It just so happens in your case that
e
should beControl.Exception.ErrorCall
sinceundefined
useserror
which throwsErrorCall
(an instance ofException
).To handle uses of
undefined
you can define something likehandleError
:It's essentially an alias
Control.Exception.handle
withe
fixed asErrorCall
which is whaterror
throws.It looks like this when run in GHCi 7.4.1:
To handle all exceptions a
handleAll
function can be written as follows:Catching all exceptions has consequences described well in this excerpt of the
Control.Exception
documentation:Source: http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Exception.html#g:4