Exception 打开输入通道的Ocaml异常处理

Exception 打开输入通道的Ocaml异常处理,exception,pattern-matching,ocaml,matching,Exception,Pattern Matching,Ocaml,Matching,作为Ocaml的初学者,我目前有以下工作代码: ... let ch_in = open_in input_file in try proc_lines ch_in with End_of_file -> close_in ch_in;; 现在我想为不存在的输入文件添加错误处理,我写了以下内容: let ch_in = try Some (open_in input_file) with _ -> None in match ch_in with | Some x ->

作为Ocaml的初学者,我目前有以下工作代码:

...
let ch_in = open_in input_file in
try
    proc_lines ch_in
with End_of_file -> close_in ch_in;;
现在我想为不存在的输入文件添加错误处理,我写了以下内容:

let ch_in = try Some (open_in input_file) with _ -> None in
match ch_in with
| Some x -> try proc_lines x with End_of_file -> close_in x
| None -> () ;;
并获取错误消息:此模式与“a选项”类型的值匹配 但此处用于匹配最后一行的exn类型的值。如果我用None代替u,我会得到一个关于不完全匹配的错误


我读到exn是异常类型。我肯定我不明白这里到底发生了什么,所以请给我指出正确的方向。谢谢

当在其他模式匹配中嵌入模式匹配时,您需要使用
(…)
开始来封装嵌入的匹配。。。结束
(括号的语法糖):


谢谢,现在我看到解析器认为“| None->()”属于“try”的匹配项
let ch_in = try Some (open_in input_file) with _ -> None in
match ch_in with
| Some x -> (try proc_lines x with End_of_file -> close_in x)
| None -> () ;;