[BLOG] OCaml Backtraces on Uncaught Exceptions, by OCamlPro

Greetings Cameleers,

Here’s another one of our heads up about our latest blog release!

Today’s topic is about an unintentionally hidden feature of the OCaml dev
environmment: backtraces on uncaught exception!

We believe this will be old news to the veteran OCaml devs but could be of much
use to the newer Cameleers out there!

Hopefully, you will learn a thing or two from reading this short article, we welcome all feedback in this very thread, thank you for reading!

Kind regards,
The OCamlPro Team

10 Likes

If you absolutely need to catch all exceptions, a last resort is to explicitely re-raise “uncatchable” exceptions:

let this_is_a_last_resort =
  try .. with
  | (Sys.Break | Assert_failure _ | Match_failure _) as e -> raise e
  | _ -> ..

I would add to this “uncatchables” list:

1 Like

The standard library should probably provide a predicate that defines whether an exception is ‘uncatchable’ or not. Eg apparently Invalid_argument is not supposed to be caught either.