Is there a way to catch only non-fatal exceptions? There are some exceptions that should not be caught in normal userland code, eg out of memory, stack overflow. In Scala we can do:
try something() catch
case NonFatal(ex) => ...
Which matches against only exceptions outside this set of ‘fatal’ exceptions. Is there something similar in OCaml? I suspect no but just wanted to double-check.
Also slightly unrelated question but, I could have sworn that the Stdlib had gained a new Exn
module for exception-related functionality, but now I can’t find it
3 Likes
Not sure about the main question, but Jane Street Base
has an Exn
module. Printexc
in the standard library has functions related to exceptions that go beyond printing. Not sure what else you could be thinking of.
1 Like
try something () with
| e when is_fatal e -> reraise e
| e -> …
I assume is_fatal
is trivially defined and thus left as an exercise for the reader
1 Like
Are you maybe looking for Fun.protect
or for Printexc.*
?
In Lwt there’s a global switch so that is_fatal
becomes function Out_of_memory | Stack_overflow -> true | _ -> false
.
See Should out-of-memory ever be caught in libraries/framework?
1 Like
Thanks. Apparently Invalid_argument
is not supposed to be caught either: OCaml library : Stdlib
1 Like