[Haskell-cafe] Extensible Exceptions

David F. Place d at vidplace.com
Sat Nov 22 09:49:34 EST 2008

On Sat, 2008-11-22 at 11:33 +0000, Thomas Schilling wrote:
> Be careful, though.  This only works if there's a single constructor
> for your exception type. If there are multiple, you should write it
> like this:
>   thing_to_try `catch` \(e :: MyErrorType) -> case e of MyError1 _ ->
> ..; MyError2 _ -> ...
> If you write `catch` (MyError1 ...) and a MyError2 is thrown, you will
> get a pattern match error exception.

Since I am trying to replicate the behavior of errorCalls, I have only
the single constructor ErrorCall to worry about.  I don't understand
though, how this works:  I have a predicate

        errorCalls e@(ErrorCall _) = Just e

In the following transcript it seems to work correctly even if something
beside ErrorCall is thrown.   Passing NonTermination to errorCalls get a
type error as expected.  Why does it work inside tryJust?

        *Main> tryJust errorCalls $ print $ [] !! 23
        tryJust errorCalls $ print $ [] !! 23^JLeft Prelude.(!!): index
        too large
        *Main> tryJust errorCalls $ print $ throw NonTermination
        tryJust errorCalls $ print $ throw NonTermination^J***
        Exception: <<loop>>
        *Main> errorCalls (ErrorCall "What?")
        errorCalls (ErrorCall "What?")^JJust What?
        *Main> errorCalls NonTermination
        errorCalls NonTermination^J
            Couldn't match expected type `ErrorCall'
                   against inferred type `NonTermination'
            In the first argument of `errorCalls', namely
            In the expression: errorCalls NonTermination
            In the definition of `it': it = errorCalls NonTermination

More information about the Haskell-Cafe mailing list