How to define a custom exception in Haskell?

The documentation for Control.Exception states that I can do the following to create my own exception:

data MyException = ThisException | ThatException deriving (Show, Typeable) instance Exception MyException 

If I insert this into a file and compile (after importing Control.Exception and Data.Typeable), I get:

 exp.hs:6:20: Can't make a derived instance of `Typeable MyException' (You need -XDeriveDataTypeable to derive an instance for this class) In the data type declaration for `MyException' 

Should I include this extension in order to have user-defined exceptions? If not, please provide an example. Thanks.

+6
exception haskell
source share
1 answer

Yes, you need to enable this extension. Typeable to write a Typeable instance manually is not recommended because it has some connections with the internal components of the GHC.

+5
source share

All Articles