在下面的代码中,我想读取一个文件并返回所有行;如果存在 IO 错误,我希望程序退出并将错误消息打印到控制台。但程序仍然遇到未处理的异常。对此的最佳实践是什么? (我想我不需要Some/None
因为无论如何我都希望程序在错误时退出。)谢谢。
let lines =
try
IO.File.ReadAllLines("test.txt")
with
| ex -> failwithf " %s" ex.Message
您可以进行类型测试模式匹配。
let lines =
try
IO.File.ReadAllLines("test.txt")
with
| :? System.IO.IOException as e ->
printfn " %s" e.Message
// This will terminate the program
System.Environment.Exit e.HResult
// We have to yield control or return a string array
Array.empty
| ex -> failwithf " %s" ex.Message
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)