Full standard library replacement for OCaml
Module Base . Backtrace . Exn
val am_recording : unit -> bool
val set_recording : bool -> unit
val with_recording : bool -> f:( unit -> 'a ) -> 'a
val most_recent : unit -> t

most_recent () returns a backtrace containing the stack that was unwound by the most recently raised exception.

Normally this includes just the function calls that lead from the exception handler being set up to the exception being raised. However, due to inlining, the stack frame that has the exception handler may correspond to a chain of multiple function calls. All of those function calls are then reported in this backtrace, even though they are not themselves on the path from the exception handler to the "raise".

val most_recent_for_exn : Exn.t -> t option

most_recent_for_exn exn returns a backtrace containing the stack that was unwound when raising exn if exn is the most recently raised exception. Otherwise it returns None.

Note that this may return a misleading backtrace instead of None if different raise events happen to raise physically equal exceptions. Consider the example below. Here if e = Not_found and g usees Not_found internally then the backtrace will correspond to the internal backtrace in g instead of the one used in f, which is not desirable.

try f () with
| e ->
  g ();
  let bt = Backtrace.Exn.most_recent_for_exn e in