开发者

How to define a custom exception in Haskell?

开发者 https://www.devze.com 2022-12-20 02:03 出处:网络
The Contro开发者_如何学运维l.Exception documentation says I can do the following to create my own exception:

The Contro开发者_如何学运维l.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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号