【发布时间】:2015-11-10 01:21:45
【问题描述】:
我正在编写一些关于在 SML 中读写文件的简单程序,并且我正在考虑跟踪文件是否成功打开/发生溢出等。如果在编译过程中引发任何异常,我希望函数返回 false 否则为 true。
这样的实现可行吗?如果是这样,怎么做?如果没有,还有其他解决方案吗?
我不确定我是否正确:
fun getN(file) =
let
val input = TextIO.openIn file
fun num(n) =
case n of
NONE => []
| SOME(str) => str_to_num(str) @ num(TextIO.inputLine input)
in
if OS.FileSys.access(file, []) then
num(TextIO.inputLine input) before TextIO.closeIn input
else []
//OR
num(TextIO.inputLine input) before TextIO.closeIn input
handle Io => []
end;
但是,当文件的目录不存在时,这些解决方案都不会返回 []。为什么?
【问题讨论】:
-
"如果在编译过程中引发任何异常" 你的意思是在运行时?查看
handle声明。