wko*_*ing 2 windows haskell exception
当我在ghci上输入以下内容时,会引发异常:
Prelude> import Control.Exception
Prelude Control.Exception> readFile "test.txt" `catch` (const $ return "exception caught!" :: SomeException -> IO String)
"*** Exception: test.txt: hGetContents: invalid argument (invalid byte sequence)
Run Code Online (Sandbox Code Playgroud)
我不明白为什么没有抓住异常.我stack ghci在Windows 7上运行上面的命令.("test.txt"文件包含一些以UTF8编码的随机日文字母,但我希望异常应该被捕获)
有人可以解释原因吗?
由于readFile返回一个惰性String,它将不会评估文件内容,直到它被使用,在这种情况下,当ghci打印它.您可以通过强制评估其内容来捕获它.
import Control.Exception
import Control.DeepSeq
(readFile "test.txt" >>= evaluate . force) `catch` (const $ return "exception caught!" :: SomeException -> IO String)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
74 次 |
| 最近记录: |