[Haskell-cafe] Re: Takusen - error handling and DBM monad

Al Falloon afalloon at synopsys.com
Wed Jan 31 13:43:48 EST 2007


Paul Moore wrote:
> Now, I can protect my query with something like
> 
> main = do
>   withSession (connect "..." "..." "...") ( do
>     catchDB (do
>       -- simple query, returning reversed list of rows.
>       r <- doQuery (sql "select username from all_users") query1Iteratee []
>       liftIO $ putStrLn $ show r
>       )
>       catcher
>     )
> 
> So far, so good. The syntax is messy, but a little refactoring should 
> fix that.
> 
> But this doesn't catch errors in the connect call.
> 
> Wrapping the withSession in catchDB doesn't work, as withSession is in
> the IO monad [1], not the DBM one. But using catch with catcher
> doesn't work, as that expects an error handler in the IO monad, but
> catcher is in the DBM monad.

You can catch exceptions in the IO monad using 'catch' from the prelude:
http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Acatch

Unfortunately, you will have to write another error handler though, 
because the catch expects the handler to have type (IOError -> IO a) 
instead of (DBException -> DBM ...).

Using both catch and catchDB should let you handle all the errors. BTW, 
what does withSession return if there is a DBException?



More information about the Haskell-Cafe mailing list