Ocaml Exceptions With Try Catch

Ocaml Exceptions With Try Catch

Its not clear what youre asking. But I suspect your TA has told you what you need to know. You can read about the OCaml try ... with expression in Section 6.7 of the OCaml manual. This is the OCaml equivalent to try ... catch of some other languages.

If you want to catch a particular exception but let others propagate as usual, you just need to match the one youre interested in:

try
    <expression1>
with Not_found -> <expression2>

If you have complicated requirements, you can match several different exceptions, and after the match you can compute values and/or re-raise exceptions.

try
    <expression1>
with
| Not_found -> raise (MLFailure abc)
| Divide_by_zero -> max_int
| _ -> raise (Invalid_argument def)

Ocaml Exceptions With Try Catch

Leave a Reply

Your email address will not be published.