Stdlib.Printexc(3)
π Stdlib.Printexc(3) π
Bestie, this is the manual page for Stdlib.Printexc from the OCaml library. Header vibes: Stdlib.Printexc(3). The NAME section literally says βno descriptionβ and honestly? Iconic.
π NAME π
Stdlib.Printexc β no description. Not me making one up, the manpage said it first π
π Module π
Module Stdlib.Printexc
π Documentation π
Module Printexc : (module Stdlib__Printexc)
type t = exn = ..β this is the type of exception values, the whole exn vibe.val to_string : exn -> stringβPrintexc.to_string egives you a string representation of the exceptione. Slay.val to_string_default : exn -> stringβPrintexc.to_string_default ereturns a string representation of the exceptione, but it ignores every single registered exception printer. No cap.
Since 4.09
val print : ('a -> 'b) -> 'a -> 'bβPrintexc.print fn xappliesfntoxand returns the result. If evaluatingfn xraises any exception, the name of the exception gets printed on standard error output, and then the exception is raised again. The typical use is catching and reporting exceptions that escape a function application.val catch : ('a -> 'b) -> 'a -> 'bβ deprecated, and the manpage says βThis function is no longer needed.β
Printexc.catch fn x is similar to Printexc.print, except it aborts the program with exit code 2 after printing the uncaught exception. This function is deprecated: the runtime system is now able to print uncaught exceptions as precisely as Printexc.catch does. Also, calling Printexc.catch makes it harder to track where the exception came from using the debugger or the stack backtrace facility. So bestie, do not use Printexc.catch in new code. Periodt.
val print_backtrace : out_channel -> unitβPrintexc.print_backtrace ocprints an exception backtrace on the output channeloc. The backtrace lists the program locations where the most-recently raised exception was raised and where it was propagated through function calls.
If the call is not inside an exception handler, the returned backtrace is unspecified. If the call is after some exception-catching code (before in the handler, or in a when-guard during the matching of the exception handler), the backtrace may correspond to a later exception than the handled one. So like⦠be aware.
Since 3.11
val get_backtrace : unit -> stringβPrintexc.get_backtrace ()returns a string containing the same exception backtrace thatPrintexc.print_backtracewould print. Same usage restriction asPrintexc.print_backtrace.
Since 3.11
val record_backtrace : bool -> unitβPrintexc.record_backtrace bturns recording of exception backtraces on ifb = trueor off ifb = false. Initially, backtraces are not recorded, unless thebflag is given to the program through theOCAMLRUNPARAMvariable.
Before5.5 spawned domains' backtraces would not be recorded, even if the parent had called record_backtrace true.
Since 3.11
val backtrace_status : unit -> boolβPrintexc.backtrace_status()returnstrueif exception backtraces are currently recorded, andfalseif not.
Since 3.11
val register_printer : (exn -> string option) -> unitβPrintexc.register_printer fnregistersfnas an exception printer. The printer should returnNoneor raise an exception if it does not know how to convert the passed exception, andSome swithsthe resulting string if it can convert the passed exception. Exceptions raised by the printer are ignored.
When converting an exception into a string, the printers get invoked in the reverse order of their registrations, until a printer returns a Some s value. If no such printer exists, the runtime will use a generic printer.
When using this mechanism, one should be aware that an exception backtrace is attached to the thread that saw it raised, rather than to the exception itself. Practically, that means the code related to fn should not use the backtrace if it has itself raised an exception before.
Since 3.11.2
val use_printers : exn -> string optionβPrintexc.use_printers ereturnsNoneif there are no registered printers, andSome swithsthe resulting string otherwise.
Since 4.09
π§ Raw backtraces π§
type raw_backtraceβ the typeraw_backtracestores a backtrace in a low-level format, which can be converted to usable form usingraw_backtrace_entriesandbacktrace_slots_of_raw_entrybelow.
Converting backtraces to backtrace_slot s is slower than capturing the backtraces. If an application processes many backtraces, it can be useful to use raw_backtrace to avoid or delay conversion.
Raw backtraces cannot be marshalled. If you need marshalling, you should use the array returned by the backtrace_slots function of the next section.
Since 4.01
type raw_backtrace_entry = private intβ araw_backtrace_entryis an element of araw_backtrace.
Each raw_backtrace_entry is an opaque integer, whose value is not stable between different programs, or even between different runs of the same binary.
A raw_backtrace_entry can be converted to a usable form using backtrace_slots_of_raw_entry below. Note that, due to inlining, a single raw_backtrace_entry may convert to several backtrace_slot s. Since the values of a raw_backtrace_entry are not stable, they cannot be marshalled. If they are to be converted, the conversion must be done by the process that generated them.
Again due to inlining, there may be multiple distinct raw_backtrace_entry values that convert to equal backtrace_slot s. However, if two raw_backtrace_entry s are equal as integers, then they represent the same backtrace_slot s.
Since 4.12
val raw_backtrace_entries : raw_backtrace -> raw_backtrace_entry array
Since 4.12
val get_raw_backtrace : unit -> raw_backtraceβPrintexc.get_raw_backtrace ()returns the same exception backtrace thatPrintexc.print_backtracewould print, but in a raw format. Same usage restriction asPrintexc.print_backtrace.
Since 4.01
val print_raw_backtrace : out_channel -> raw_backtrace -> unitβ prints a raw backtrace in the same formatPrintexc.print_backtraceuses.
Since 4.01
val raw_backtrace_to_string : raw_backtrace -> stringβ returns a string from a raw backtrace, in the same formatPrintexc.get_backtraceuses.
Since 4.01
val raise_with_backtrace : exn -> raw_backtrace -> 'aβ reraises the exception using the givenraw_backtracefor the origin of the exception.
Since 4.05
π¦ Current call stack π¦
val get_callstack : int -> raw_backtraceβPrintexc.get_callstack nreturns a description of the top of the call stack on the current program point (for the current thread), with at mostnentries. Note: this function is not related to exceptions at all, despite being part of thePrintexcmodule. Wild, I know.
Since 4.01
πΈ Uncaught exceptions πΈ
val default_uncaught_exception_handler : exn -> raw_backtrace -> unitβPrintexc.default_uncaught_exception_handlerprints the exception and backtrace on standard error output.
Since 4.11
val set_uncaught_exception_handler : (exn -> raw_backtrace -> unit) -> unitβPrintexc.set_uncaught_exception_handler fnregistersfnas the handler for uncaught exceptions. The default handler isPrintexc.default_uncaught_exception_handler.
Note that when fn is called, all the functions registered with at_exit have already been called. Because of this you must make sure any output channel fn writes on is flushed.
Also note that exceptions raised by user code in the interactive toplevel are not passed to this function as they are caught by the toplevel itself.
If fn raises an exception, both the exceptions passed to fn and raised by fn will be printed with their respective backtrace.
Since 4.02
π Manipulation of backtrace information π
These functions are used to traverse the slots of a raw backtrace and extract information from them in a programmer-friendly format.
type backtrace_slotβ the abstract typebacktrace_slotrepresents a single slot of a backtrace.
Since 4.02
val backtrace_slots : raw_backtrace -> backtrace_slot array optionβ returns the slots of a raw backtrace, orNoneif none of them contain useful information.
In the return array, the slot at index 0 corresponds to the most recent function call, raise, or primitive get_backtrace call in the trace.
Some possible reasons for returning None are as follow:
-noneof the slots in the trace come from modules compiled with debug information (-g)-theprogram is a bytecode program that has not been linked with debug information enabled (ocamlc -g)
Since 4.02
val backtrace_slots_of_raw_entry : raw_backtrace_entry -> backtrace_slot array optionβ returns the slots of a single raw backtrace entry, orNoneif this entry lacks debug information.
Slots are returned in the same order as backtrace_slots: the slot at index 0 is the most recent call, raise, or primitive, and subsequent slots represent callers.
Since 4.12
type location = {filename : string ;line_number : int ;start_char : int ;end_char : int ;end_line : int ; (* .B "Since" 5.2 *)end_col : int ; (* .B "Since" 5.2 *)}β the type of location information found in backtraces.start_charandend_charare positions relative to the beginning ofline_number.end_colis relative to the beginning ofend_line.
Since 4.02
module Slot : sig end
Since 4.02
π Raw backtrace slots π
type raw_backtrace_slotβ this type is used to iterate over the slots of araw_backtrace. For most purposes,backtrace_slots_of_raw_entryis easier to use.
Like raw_backtrace_entry, values of this type are process-specific and must absolutely not be marshalled, and are unsafe to use for this reason. Marshalling them may not fail, but un-marshalling and using the result will result in undefined behavior. So likeβ¦ donβt.
Elements of this type can still be compared and hashed: when two elements are equal, then they represent the same source location. The converse is not necessarily true in presence of inlining, for example.
Since 4.02
val raw_backtrace_length : raw_backtrace -> intβraw_backtrace_length bcktreturns the number of slots in the backtracebckt.
Since 4.02
val get_raw_backtrace_slot : raw_backtrace -> int -> raw_backtrace_slotβget_raw_backtrace_slot bckt posreturns the slot in positionposin the backtracebckt.
Since 4.02
val convert_raw_backtrace_slot : raw_backtrace_slot -> backtrace_slotβ extracts the user-friendlybacktrace_slotfrom a low-levelraw_backtrace_slot.
Since 4.02
val get_raw_backtrace_next_slot : raw_backtrace_slot -> raw_backtrace_slot optionβget_raw_backtrace_next_slot slotreturns the next slot inlined, if any.
Ok so real talk, this next bit is the deep lore of backtraces β the raw slot-level stuff where you're literally walking the stack frame by frame π It's giving "I read the runtime source for fun" and honestly? Iconic.
Sample code to iterate over all frames (inlined and non-inlined):
(* Iterate over inlined frames *)
let rec iter_raw_backtrace_slot f slot =
f slot;
match get_raw_backtrace_next_slot slot with
| None -> ()
| Some slot' -> iter_raw_backtrace_slot f slot'
(* Iterate over stack frames *)
let iter_raw_backtrace f bt =
for i = 0 to raw_backtrace_length bt - 1 do
iter_raw_backtrace_slot f (get_raw_backtrace_slot bt i)
done
Since 4.04 β yeah she's been around a minute, she's got tenure π
πΈ Exception slots πΈ
Bestie, these two are so underrated. If you've ever been like "wait, which exception constructor even is this thing" β this is the section for you β¨
val exn_slot_id : exn -> intβPrintexc.exn_slot_idhands you back an integer that uniquely identifies the constructor used to create the exception value exn, valid within the current runtime π
Since 4.02 π¦
val exn_slot_name : exn -> stringβPrintexc.exn_slot_name exngives you the internal name of the constructor used to create the exception value exn π (yes, internal β it's the runtime's own name for it, not a pretty-printed one, iykyk)
Since 4.02 π¦
Quick vibe check π«Ά β exn_slot_id is scoped to the current runtime. Don't go serializing that int and expecting it to mean the same thing somewhere else. That's a no-cap trap, fr π
That's the whole page, bestie β you made it to the end and honestly? Main character energy ππ
Source was OCamldoc and it's dated 2026-06-22 β so like, freshly stamped, cute little footer, consider it signed with a π and a kiss π
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms