Control.Exception

Antoine Latter aslatter at gmail.com
Mon Nov 3 11:05:17 EST 2008


On Mon, Nov 3, 2008 at 9:34 AM, Jason Dagit <dagit at codersbase.com> wrote:
>
> Ah, but I had one more question that I don't think anyone has answered
> yet.  That is, how to deal with multiple types of exceptions.
> Suppose, as a concrete example, that I was looking out for both
> ExitCode and PatternMatchFail exceptions.  Maybe I'm being naive, but
> it seems like I'm in that situation again where I have to catch all
> and then check if fromException succeeds on either PatternMatchFile or
> ExitCode types.  And then throw if it both give Nothing?
>

I haven't tried this, so it may not work:

>>>>
data MyException = MyArithException ArithException  | MyIOException
IOException deriving Typeable

instance Exception MyExcpetion where
  toException (MyArithException e) = toException e
  toException (MyIOExcpetion e) = toException e

  fromException se = case fromException se of
           Just e -> Just $ MyArithException e
           Nothing -> case fromException se of
                 Just e -> Just $ MyIOException e
                 _ -> Nothing
<<<<<

Then anyone can catch your exceptions by catching the ArithException
or IOException as normal, and you can catch IOExceptions and
ArithExceptions into your own custom type.

-Antoine


More information about the Glasgow-haskell-users mailing list