Issues with new Control.Exception

Bertram Felgenhauer bertram.felgenhauer at
Wed Jan 28 17:02:43 EST 2009

John Goerzen wrote:
>  * Catch doesn't work in many common cases anymore.  In particular,
>    the example right there in haddock is broken:
>       catch (openFile f ReadMode)
>        (\e -> hPutStr stderr ("Couldn't open "++f++": " ++ show e))
>    That's broken, of course, because the type of e isn't fixed.
>    But it's also broken because openFile has a different return type
>    than hPutStr.

Which means it has been broken for quite some time.

>  * I like the new system overall, but it is annoying that
>    we can't do the sort of catch that is contemplated in the example
>    above anymore.  This is especially annoying for me in several cases
>    where I want to catch *any* exception and do things with it,
>    especially when doing unit testing.

You can actually do that - all you have to do is catch SomeException, say

    openFile f ReadMode `catch` \e -> do
        hPutStrLn stderr $
           "Couldn't open " ++ f ++ ": " ++ show (e :: SomeException)
        throw e

This works because SomeException is an instance of the Exception class
with the obvious trivial implementations for toException and
fromException. It should be documented, of course.

onException does basically that - so it works for any exceptions:

    onException :: IO a -> IO b -> IO a
    onException io what = io `catch` \e -> do what
                                              throw (e :: SomeException)



More information about the Libraries mailing list