让我们做出以下假设:
我如何找出该例外的类型?
最小的例子:
main = error "foo"
Run Code Online (Sandbox Code Playgroud)
(这是当然的ErrorCall
,但你无法从错误信息中得知.)
Die*_*Epp 17
是.假设您使用新的异常API,所有Exception
类型必须是实例Typeable
.
import Control.Exception
import Data.Typeable
import Prelude hiding (catch)
realMain = error "example"
main = realMain `catch` h where
h (SomeException e) = do
putStrLn $ "Caught exception of type " ++ show (typeOf e)
putStrLn $ show e
Run Code Online (Sandbox Code Playgroud)
结果:
Caught exception of type GHC.Exception.ErrorCall example