The Control.Exception documentation says I can do the following to create my own exception:
data MyException = ThisException | ThatException
deriving (Show, Typeable)
instance Exception MyException
If I paste 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'
Must I turn on this extension in order to have user-defined exceptions? If not, someone please provide an example. Thanks.
There are three different ways exceptions can be thrown in Haskell: Synchronously thrown: an exception is generated from IO code and thrown inside a single thread. Asynchronously thrown: an exception is thrown from one thread to another thread to cause it to terminate early.
try :: Exception e => IO a -> IO (Either e a) try takes an IO action to run, and returns an Either . If the computation succeeded, the result is given wrapped in a Right constructor. (Think right as opposed to wrong). If the action threw an exception of the specified type, it is returned in a Left constructor.
Yes, you need to turn on that extension. It's not a good idea to try to write the Typeable
instance by hand because it has some ties to the internals of GHC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With