# Catching non-fatal exceptions?

**URL:** <https://discuss.ocaml.org/t/catching-non-fatal-exceptions/14484>\
**Category:** Learning\
**Created:** [April 13, 2024, 7:40pm UTC](https://discuss.ocaml.org/t/catching-non-fatal-exceptions/14484 "2024-04-13T19:40:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [April 13, 2024, 7:40pm UTC](https://discuss.ocaml.org/t/catching-non-fatal-exceptions/14484/1 "2024-04-13T19:40:49Z")

</div>

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:

```auto
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 🤔

---

<div class="post-metadata">

**Author:** ![ninjaaron](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ninjaaron/32/1885_2.png) [@ninjaaron](https://discuss.ocaml.org/u/ninjaaron)\
**Post date:** [April 15, 2024, 9:07am UTC](https://discuss.ocaml.org/t/catching-non-fatal-exceptions/14484/2 "2024-04-15T09:07:49Z")

</div>

> [@yawaramin](#):
>
> 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 🤔

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.

---

<div class="post-metadata">

**Author:** ![smolkaj](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/smolkaj/32/266_2.png) [@smolkaj](https://discuss.ocaml.org/u/smolkaj)\
**Post date:** [April 16, 2024, 2:52am UTC](https://discuss.ocaml.org/t/catching-non-fatal-exceptions/14484/3 "2024-04-16T02:52:08Z")

</div>

```auto
try something () with
| e when is_fatal e -> reraise e
| e -> …

```

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [April 16, 2024, 5:40am UTC](https://discuss.ocaml.org/t/catching-non-fatal-exceptions/14484/4 "2024-04-16T05:40:16Z")

</div>

I assume `is_fatal` is trivially defined and thus left as an exercise for the reader 😉

---

<div class="post-metadata">

**Author:** ![raphael-proust](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/raphael-proust/32/415_2.png) [@raphael-proust](https://discuss.ocaml.org/u/raphael-proust)\
**Post date:** [April 20, 2024, 5:28pm UTC](https://discuss.ocaml.org/t/catching-non-fatal-exceptions/14484/5 "2024-04-20T17:28:33Z")

</div>

> [@yawaramin](#):
>
> I could have sworn that the Stdlib had gained a new `Exn` module for exception-related functionality, but now I can’t find it

Are you maybe looking for `Fun.protect` or for `Printexc.*`?

---

<div class="post-metadata">

**Author:** ![raphael-proust](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/raphael-proust/32/415_2.png) [@raphael-proust](https://discuss.ocaml.org/u/raphael-proust)\
**Post date:** [April 20, 2024, 5:30pm UTC](https://discuss.ocaml.org/t/catching-non-fatal-exceptions/14484/6 "2024-04-20T17:30:14Z")

</div>

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?](https://discuss.ocaml.org/t/should-out-of-memory-ever-be-caught-in-libraries-framework/10712)

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [April 20, 2024, 5:58pm UTC](https://discuss.ocaml.org/t/catching-non-fatal-exceptions/14484/7 "2024-04-20T17:58:04Z")

</div>

Thanks. Apparently `Invalid_argument` is not supposed to be caught either: [OCaml library : Stdlib](https://ocaml.org/manual/5.1/api/Stdlib.html#EXCEPTIONInvalid_argument)
