[Haskell-cafe] Re: Control.Exception try and catch

Simon Marlow marlowsd at gmail.com
Wed Apr 28 17:01:59 EDT 2010


On 28/04/10 14:45, Mads Lindstrøm wrote:
> Hi
>
> From
> http://haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/Control-Exception.html#3
>
> "... The difference between using try and catch for recovery is that in
> catch the handler is inside an implicit block (see "Asynchronous
> Exceptions") which is important when catching asynchronous
> exceptions ..."
>
> However, 'try' is implemented by calling catch
> http://haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/src/Control-Exception-Base.html#try:
>
> try :: Exception e =>  IO a ->  IO (Either e a)
> try a = catch (a>>= \ v ->  return (Right v)) (\e ->  return (Left e))
>
> Thus, I wonder, why do 'try' not "inherit" the implicit block mentioned above?

There's nothing magic going on - the "handler" in the case of try is 
just (return . Left), and that does indeed get executed with an implicit 
block.

> Looking at catch:
>
> catch   :: Exception e
>          =>  IO a         -- ^ The computation to run
>          ->  (e ->  IO a)  -- ^ Handler to invoke if an exception is raised
>          ->  IO a
> catch io h = H'98.catch  io  (h . fromJust . fromException . toException)
>
>
> I see no call to 'block'. But maybe it is hidden in H'98.catch? And is H'98.catch == Prelude.catch ?

The block is implicit (it's built into the implementation of catch, in 
fact).

Cheers,
	Simon


More information about the Haskell-Cafe mailing list