[Haskell] Better Exception Handling

Lennart Augustsson lennart at augustsson.net
Thu Nov 25 13:52:43 EST 2004


John Goerzen wrote:
> 
> So why do we have:
> 
> catchJust :: (Exception -> Maybe b) -> IO a -> (b -> IO a) -> IO a
> 
> instead of:
> 
> catchJust :: (Exception -> Maybe b) -> (c -> a) -> c -> (b -> a) -> a

As I'm sure you have gathered from all the answers you can't have the
latter and keep Haskell pure.  But there is an interesting alternative
(at least theoretically).  You could have a function like

mkCatchJust :: IO ((Exception -> Maybe b) -> (c -> a) -> c -> (b -> a) -> a)

And you would use it by

     do
	cj <- mkCatchJust

	....  cj ....

The cj function can be used in non-IO code and will behave just as you
want.  But you have to create it in the IO monad, since it's behaviour
is not deterministic (especially not in the face of async exceptions).

The tricky thing is to implement it, because cj should really only be
used once.  Why?  Because it has to behave the same way for the same
arguments every time.  This is not easily implemented. :(

	-- Lennart



More information about the Haskell mailing list