我正在尝试将我的程序的错误处理转换为使用异常。我建立了一个REPL来控制我的模拟,我也处理错误:
> repl :: Sim -> IO Sim
> repl old_sim = do
> new_sim <- E.catch (do line <- getLine
> commands <- parseCommand line
> runCommands commands old_sim)
> (\err -> do putStrLn . show $ err
> return old_sim)
> if alive new_sim
> then repl new_sim
> else return new_sim
parseCommands或runCommands可以fail String
,两者都返回IO Sim
。
当我加载文件时,我收到了这个巨大的错误:
Prelude> :l simple_sim
[1 of 1] Compiling Main ( simple_sim.lhs, interpreted )
simple_sim.lhs:95:18:
Ambiguous type variable `a0' in the constraints:
(E.Exception a0) arising from a use of `E.catch'
at simple_sim.lhs:95:18-24
(Show a0) arising from a use of `show' at simple_sim.lhs:100:49-52
Probable fix: add a type signature that fixes these type variable(s)
In a stmt of a 'do' expression:
new_sim <- E.catch
(do { line <- getLine;
commands <- parseCommand line;
runCommands commands old_sim })
(\ err
-> do { putStrLn . show $ err;
return old_sim })
In the expression:
do { new_sim <- E.catch
(do { line <- getLine;
commands <- parseCommand line;
runCommands commands old_sim })
(\ err
-> do { putStrLn . show $ err;
.... });
if alive new_sim then repl new_sim else return new_sim }
In an equation for `repl':
repl old_sim
= do { new_sim <- E.catch
(do { line <- getLine;
commands <- parseCommand line;
.... })
(\ err -> do { ... });
if alive new_sim then repl new_sim else return new_sim }
Failed, modules loaded: none.
具体做法是:
Probable fix: add a type signature that fixes these type variable(s)
我会在哪里插入此类签名?
答案 0 :(得分:3)
你需要在err上确定你想要捕获的异常。