Issues with new Control.Exception

Hi everyone, Please CC me on replies as I'm not on this list. I've noticed several issues with Control.Exception, although I'm not sure I'm equipped well to fix them: * onException is undocumented * 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. * The catchJust example references errorCalls, which is no longer in Control.Exception. * 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. * I wonder if the docs to Control.Exception could benefit from a review. I also notice that the comments about catchJust may no longer apply. * What is the expected life expectancy of OldException, and how does it interact with Exception? Can I use catch from OldException to still do what I want? If so, it might be nice to note how exactly catch works with new exceptions. Some of these I may be able to submit patches for in time, but right now I'm mainly trying to understand what the expected way to use the new exceptions is, so I'm probably not the right person to do it yet. -- John

On Wed, Jan 28, 2009 at 4:00 PM, John Goerzen
* 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.
I think the correct code would be something like this:
catch (openFile f ReadMode)
(\(e::SomeException) ->
hPutStr stderr ("Couldn't open "++f++": " ++ show e)) >>
throwIO e)
Although it might be better to replace "SomeException" with "IOException".
--
Dave Menendez

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) HTH, Bertram
participants (3)
-
Bertram Felgenhauer
-
David Menendez
-
John Goerzen