[Haskell-cafe] Newbie Question about Error Handling

Gerd M gerd_m1977 at hotmail.com
Mon Jul 25 03:50:19 EDT 2005


>We're being clever and returning from IO not a Char, but a MyMonad Char
>(mimp).  Then, we run that, which either produces the answer we wanted,
>or throws an error in MyMonad.
Thanks. That did the trick! :-)
Regards

>
>However, it's still rather cumbersome, so here's a function similar to
>liftError that works for your monad.
>
>     -- I'll assume you have such a function
>     fromIOError :: IOError -> MyErrorType
>
>     -- The name is intended to convey that IO errors are caught and
>     -- reintroduced into MyMonad.  Better suggestions welcome.
>     liftIOTrap :: IO a -> MyMonad a
>     liftIOTrap io = do mx <- liftIO (do x <- io
>                                         return (return x)
>                                      `catchError`
>                                      (\e -> return (throwError
>                                                       (fromIOError e))))
>                        mx
>
>     foo :: MyMonad a
>     foo = do
>                 inp <- liftIOTrap (getChar
>                                     `catchError`
>                                     (\e -> if isEOFError e then return 
>'\0'
>                                                            else throwError 
>e))
>                 ...
>
>Andrew
>
>[1] http://haskell.org/pipermail/haskell-cafe/2005-June/010361.html
>

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/



More information about the Haskell-Cafe mailing list