[Haskell] modern language design, stone age tools

Simon Marlow simonmar at microsoft.com
Thu Jun 24 07:09:16 EDT 2004


On 24 June 2004 11:54, MR K P SCHUPKE wrote:

> With reference to "mapException", I thought this seemed a good idea.
> I like the 'AnnotatedException' idea... this is much better than
> concatenating strings... However for not I thought I would test it
> as is, but it doesn't work as I thought - perhaps someone could
> point out where I have gone wrong.
> 
> 1) this works:
> 
> main :: IO ()
> main = do
>    a <- getLine
>    hPutStrLn $ showInt (fst . head . readDec $ a :: Int)
>    `Exception.catch` (\e -> throw $ ErrorCall $ showString "Urk :" $
> show e) 
> 
> 2) this doesn't work:
> 
> main :: IO ()
> main = mapException (\x -> ErrorCall $ showString "Urk :" $ show x) $
>    do a <- getLine
>    hPutStrLn $ showInt (fst . head . readDec $ a :: Int)

This is due to the nature of exceptions in Haskell.  Evaluating the
expression (do a <- getLine; hPutStrLn ...) does not do any IO, and it
doesn't raise any exceptions, so the mapException doesn't get to
annotate any exceptions.

What you really wanted here was (untested)

 mapExceptionIO :: IO a -> (Exception -> Exception) -> IO a
 mapExceptionIO io f = catch io (\e -> throwIO (f e))

probably we should have this in Control.Exception too.

Cheers,
	Simon


More information about the Haskell mailing list