# \[BLOG\] OCaml Backtraces on Uncaught Exceptions, by OCamlPro

**URL:** <https://discuss.ocaml.org/t/blog-ocaml-backtraces-on-uncaught-exceptions-by-ocamlpro/14551>\
**Category:** Learning\
**Tags:** ocaml, exceptions, backtrace, blog\
**Created:** [April 25, 2024, 1:14pm UTC](https://discuss.ocaml.org/t/blog-ocaml-backtraces-on-uncaught-exceptions-by-ocamlpro/14551 "2024-04-25T13:14:48Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![mbarbin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mbarbin/32/4421_2.png) [@mbarbin](https://discuss.ocaml.org/u/mbarbin)\
**Post date:** [May 15, 2024, 9:40pm UTC](https://discuss.ocaml.org/t/blog-ocaml-backtraces-on-uncaught-exceptions-by-ocamlpro/14551/5 "2024-05-15T21:40:31Z")

</div>

I haven’t yet had the chance to read the blog post, but I’ve been following the ensuing discussion here (Hi Louis!). Regarding the comments about the handling of “uncatchable” exceptions, I wanted to bring up 3 examples from commonly used libraries:

`Rresult.R.trap_exn` (latest = 0.7.0):

```ocaml
  let trap_exn f v = try Ok (f v) with
  | e ->
      let bt = Printexc.get_raw_backtrace () in
      Error (`Exn_trap (e, bt))

```

`Base.Or_error.try_with` (latest = v0.17.0):

```ocaml
let try_with ?(backtrace = false) f =
  try Ok (f ()) with
  | exn -> Error (Error.of_exn exn ?backtrace:(if backtrace then Some `Get else None))
;;

```

`Stdlib.Fun.protect` (latest = 5.2)

```ocaml
let protect ~(finally : unit -> unit) work =
  let finally_no_exn () =
    try finally () with e ->
      let bt = Printexc.get_raw_backtrace () in
      Printexc.raise_with_backtrace (Finally_raised e) bt
  in
  match work () with
  | result -> finally_no_exn () ; result
  | exception work_exn ->
      let work_bt = Printexc.get_raw_backtrace () in
      finally_no_exn () ;
      Printexc.raise_with_backtrace work_exn work_bt

```

I haven’t delved deeply into these examples, but at first glance, it seems like all three might exhibit the characteristic of catching all exceptions, including those that are considered “uncatchable” as discussed here. Do you concur? Is this really an issue in practice?

Possibly related discussions:

> [@Catching non-fatal exceptions?](https://discuss.ocaml.org/t/catching-non-fatal-exceptions/14484):
>
> 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 …

> [@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):
>
> What use cases are there for catching Out\_of\_memory? What about Stack\_overflow? Should a library ever catch those exceptions? Should a framework ever catch those exceptions? As a more concrete question, should Lwt ever catch Out\_of\_memory? Currently Lwt catches all exceptions and wraps them in rejected promises. In my opinion the exceptions raised by the OCaml runtime should be treated differently and should just escape. These exceptions happen out of the blue, out of different blues on diff…

By the way, this may be off-topic for now, but I’m curious about how these exceptions would interact with an OCaml’s type system that gained the ability to express the exceptions a function can raise as part of its type. What would be the status of exceptions that are designed not to be caught?

---

_[View the full topic](https://discuss.ocaml.org/t/blog-ocaml-backtraces-on-uncaught-exceptions-by-ocamlpro/14551)._
