[Haskell-beginners] Problem with catching error

Alexey G kreed131 at gmail.com
Sun Jul 31 23:24:13 CEST 2011


Hello. I have some problem with catching error.

My function try to read value from string and if it's fails - return default
(d).
With this:
>import Control.Exception
>readIOWith :: Read a => a -> String -> IO a
>readIOWith d x = catch tryRead errHandler
>    where errHandler :: SomeException -> IO a
>              errHandler _ = return d
>              tryRead        = readIO x

I have error:

Could not deduce (a ~ a1)
    from the context (Read a)
      bound by the type signature for
                 readIOWith :: Read a => a -> String -> IO a
      at BWClub/Common/Helpers/Packets.hs:(19,1)-(22,33)
      `a' is a rigid type variable bound by
          the type signature for readIOWith :: Read a => a -> String -> IO a
          at BWClub/Common/Helpers/Packets.hs:19:1
      `a1' is a rigid type variable bound by
           the type signature for errHandler :: SomeException -> IO a1
           at BWClub/Common/Helpers/Packets.hs:21:11
    In the first argument of `return', namely `d'
    In the expression: return d
    In an equation for `errHandler': errHandler _ = return d

But this code works:
>readIOWith d x = catch tryRead (\e -> print (e :: SomeException) >> return
d)
>    where tryRead      = readIO x

Sorry for my english.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110801/13fd1da4/attachment.htm>


More information about the Beginners mailing list