Haskell - 检查文件句柄的有效性
好吧,伙计们,超级简单的问题(谷歌没有帮助我解决这个问题似乎很奇怪):
import IO
--.... yadda, yadda, yadda
file <- openFile "/some/path" ReadMode
如何检查从 openFile
获得的句柄是否是有效的句柄,即文件存在并且打开成功?
Ok, guys, super easy question (it seems weird that Google didn't help me with this one):
import IO
--.... yadda, yadda, yadda
file <- openFile "/some/path" ReadMode
How do I check if the handle that I got from openFile
is a valid handle, i.e. that the file exists and was opened successfully?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果文件不存在,或者发生其他错误,对
openFile
的调用将失败并出现异常。例如:
失败原因:
openFile
可能抛出的异常类型 此处列出, 并包括:isPermissionError 。您可以使用 Control.Exception< 捕获这些错误/a>,像这样:
If the file doesn't exist, or some other error occurs, the call to
openFile
will fail with an exception.For example:
Fails with:
The types of exception that may be thrown by
openFile
are listed here, and include:You can catch these errors using Control.Exception, like so: