[Haskell-cafe] Correct way to "catch all exceptions"
kudah
kudahkukarek at gmail.com
Wed Jul 10 13:39:29 CEST 2013
I think that new SomeAsyncException type in base is supposed to make
it possible to ignore all asynchronous exceptions.
https://github.com/ghc/packages-base/blob/master/GHC/IO/Exception.hs#L113
On Wed, 10 Jul 2013 09:28:12 +0300 Michael Snoyman
<michael at snoyman.com> wrote:
> There's a pattern that arises fairly often: catching every exception
> thrown by code. The naive approach is to do something like:
>
> result <- try someCode
> case result of
> Left (e :: SomeException) -> putStrLn $ "It failed: " ++ show
> e Right realValue -> useRealValue
>
> This seems perfectly valid, except that it catches a number of
> exceptions which seemingly should *not* be caught. In particular, it
> catches the async exceptions used by both killThread and timeout.
>
> I think it's fair to say that there's not going to be a single
> function that solves all cases correctly, but it is a common enough
> situation that people need to write code that resumes work in the
> case of an exception that I think we need to either have some
> guidelines for the right approach here, or perhaps even a utility
> function along the lines of:
>
> shouldBeCaught :: SomeException -> Bool
>
> One first stab at such a function would be to return `False` for
> AsyncException and Timeout, and `True` for everything else, but I'm
> not convinced that this is sufficient. Are there any thoughts on the
> right approach to take here?
More information about the Haskell-Cafe
mailing list