在OCaml中处理多个异常类型

Nic*_*ner 5 syntax error-handling ocaml

以下是可能的吗?

try
  (* danger zone *)
with Not_found e -> 
  (* code to handle not found *)
with t -> 
  (* code to handle all other issues *)
Run Code Online (Sandbox Code Playgroud)

如果我在顶层键入它,我会在第二个上遇到语法错误with.也许有一些我不知道的语法?

首选前置另一个try匹配的方法是with什么?

Jef*_*eld 13

with部分是一系列模式,因此您可以按如下方式编写:

try
    (* dangerous code area *)
with
    | Not_found -> (* Not found handling code *)
    | t -> (* Handle other issues here *)
Run Code Online (Sandbox Code Playgroud)


gee*_*aur 5

with是一种match表达; 你不要为多个模式重复它,而是|用来分隔每个模式 -> 表达式,就像a match.