2
我正在学习如何在Haskell中使用异常。
当试图复制在前奏this简单的例子,我得到:异常类型错误
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
Prelude> :m Control.Exception
Prelude Control.Exception> let x = 5 `div` 0
Prelude Control.Exception> let y = 5 `div` 1
Prelude Control.Exception> print x
*** Exception: divide by zero
Prelude Control.Exception> print y
5
Prelude Control.Exception> try (print x)
<interactive>:16:1:
No instance for (Show (IO (Either e0())))
arising from a use of `print'
In a stmt of an interactive GHCi command: print it
Prelude Control.Exception>
为什么我得到没有实例错误上try(print x)
,当我以前有一个例外?
问题是** GHCi **不知道'e0'的类型,所以你必须告诉:'尝试(打印x):: IO(ArithException()) - 原因是在编译时有很多可能的实例(对于不同的例外:[见这里](https://hackage.haskell.org/package/) base-4.8.1.0/docs/Control-Exception-Base.html#t:Exception) - 和GHCi不能选择) – Carsten
(当然你也可以总是使用'SomeException') – Carsten
@Carsten谢谢 – Ionut