[Haskell-cafe] Correct way to "catch all exceptions"

Ertugrul Söylemez es at ertes.de
Wed Jul 10 10:29:25 CEST 2013


Michael Snoyman <michael at snoyman.com> wrote:

>     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?

I think there is no one right approach.  However, if you add such a
function to the exception library, it really belongs into the Exception
type class with the following type:

    shouldBeCaught :: (Exception e) => e -> Bool

However, a better approach is to have exception tags.  In most cases you
don't want to catch killThread's or timeout's exception, but you do want
to catch all error exceptions:

    data Tag = Error | Abort | TryAgain | {- ... -} | Other String
        deriving (Data, Eq, Ord, Read, Show, Typeable)

    instance IsString Tag where
        fromString t = Other t

This could then manifest in the following two functions in the Exception
type class:

    hasTag :: (Exception e) => Tag -> e -> Bool
    tagsOf :: (Exception e) => e -> [Tag]

Then exception catchers (functions that risk swallowing important
exceptions) could filter by type and tag.


Greets,
Ertugrul

-- 
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130710/a631a0b8/attachment.pgp>


More information about the Haskell-Cafe mailing list