
2008/11/22 David F. Place
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>>
It doesn't. The last line is printed by GHCi. Note the missing "Exception: " in the first call.