【发布时间】:2013-11-30 14:53:29
【问题描述】:
我正在玩一些在线示例,其中一个有这样的短语:
do ... contents <- getDirectoryContents path `catch` const (return [])
但它不会为我编译,并给出错误:
No instance for (Exception e0) arising from a use of `catch'
The type variable `e0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Exception NestedAtomically
-- Defined in `Control.Exception.Base'
instance Exception NoMethodError
-- Defined in `Control.Exception.Base'
instance Exception NonTermination
-- Defined in `Control.Exception.Base'
...plus 7 others
In a stmt of a 'do' block:
contents <- getDirectoryContents path `catch` const (return [])
In the expression:
do { contents <- getDirectoryContents path
`catch` const (return []);
我不得不更改它以提供处理程序的类型,现在可以,但有点混乱:
contents <- getDirectoryContents path `catch` (\(SomeException e) -> const (return []) e)
所以我想知道为什么我需要进行此更改,以及这是否是最简洁的方法。 (是的,我知道我可以使用其他形式的 try、handle、...)
【问题讨论】:
标签: exception haskell exception-handling