Handling Multiple Exception Types in OCaml

Is the following possible?

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

If I type this in the top layer, I will get a syntax error in the second with. Perhaps there is some kind of syntax that I don't know about?

Is the preferred method to add another tryto match each with?

+5
source share
2 answers

The part withis a series of templates, so you can write it as follows:

try
    (* dangerous code area *)
with
    | Not_found -> (* Not found handling code *)
    | t -> (* Handle other issues here *)
+13
source

with- expression match; you do not repeat it for multiple patterns, instead you use |to separate each expression ->, as with match.

+5
source

All Articles