[Haskell-cafe] How to catch all exceptions that could be caught?

Gregory Crosswhite gcrosswhite at gmail.com
Thu Jan 12 08:23:44 CET 2012


On 01/12/12 17:07, Simon Hengel wrote:
> I think there are situation when it is justified to catch almost all
> exceptions.  And people do that a lot, which often leads to ctrl-c not
> properly working (e.g. we had this in HUnit before 1.2.4.2). 

Indeed, and in fact this situation is a very natural occurrence whenever
you are writing code that takes an arbitrary IO action, executes it, and
then returns either the result or the exception that it threw. The code
that I last used for this took advantage of catchJust and looked roughly
like the following:

execute :: IO a → IO (Either SomeException a)
execute action =
catchJust
(\e → case fromException e of {Just (_ :: AsyncException) → Nothing; _ →
Just e})
(Right <$> action)
(return . Left)

Cheers,
Greg



More information about the Haskell-Cafe mailing list