How do I create dynamic exceptions

Derek Elkins ddarius@hotpop.com
Tue, 22 Jul 2003 08:25:07 -0400


On Tue, 22 Jul 2003 12:51:24 +0100
"Bayley, Alistair" <Alistair_Bayley@ldn.invesco.com> wrote:

> Exceptions, again...
> 
> The docs for Control.Exception say (for Dynamic exceptions): "When
> using dynamic exceptions it is advisable to define a new datatype to
> use for your exception type, to avoid possible clashes with dynamic
> exceptions used in other libraries."
> 
> So I went with that and created a datatype for my exceptions:
> 
> > data MyException = MkExc Int String
> 
> ... but now it seems I have to install this as an instance of
> Typeable, and I don't see how to do that.
> 
> > instance Typeable MyException where
> >   typeOf ? = ?

The documentation for Data.Dynamic is pretty clear for this and I
believe with GHC6 you can derive Typeable.

{-# NOINLINE myExceptionTyCon #-}
-- the documentation suggests a fully qualified name
myExceptionTyCon = mkTyCon "MyException" 

instance Typeable MyException where
    typeOf _ = mkAppTy myExceptionTyCon []