Module Base.Exn
Exceptions.
sexp_of_t uses a global table of sexp converters. To register a converter for a new exception, add [@@deriving_inline sexp][@@@end] to its definition. If no suitable converter is found, the standard converter in Printexc will be used to generate an atomic S-expression.
exceptionFinally of t * tRaised when finalization after an exception failed, too. The first exception argument is the one raised by the initial function, the second exception the one raised by the finalizer.
exceptionReraised of string * t
val create_s : Sexp.t -> tcreate_s sexpreturns an exceptiontsuch thatphys_equal (sexp_of_t t) sexp. This is useful when one wants to create an exception that serves as a message and the particular exn constructor doesn't matter.
val raise_without_backtrace : t -> _Same as
raise, except that the backtrace is not recorded.
val reraise : t -> string -> _val reraisef : t -> ('a, unit, string, unit -> _) Stdlib.format4 -> 'aTypes with
format4are hard to read, so here's an example.let foobar str = try ... with exn -> Exn.reraisef exn "Foobar is buggy on: %s" str ()
val to_string : t -> stringHuman-readable, multi-line.
val to_string_mach : t -> stringMachine format, single-line.
val protectx : f:('a -> 'b) -> 'a -> finally:('a -> unit) -> 'bExecutes
fand afterwards executesfinally, whetherfthrows an exception or not.
val protect : f:(unit -> 'a) -> finally:(unit -> unit) -> 'aval handle_uncaught : exit:bool -> (unit -> unit) -> unithandle_uncaught ~exit fcatches an exception escapingfand prints an error message to stderr. Exits with return code 1 ifexitistrue, and returns unit otherwise.Note that since OCaml 4.02.0, you don't need to use this at the entry point of your program, as the OCaml runtime will do better than this function.
val handle_uncaught_and_exit : (unit -> 'a) -> 'ahandle_uncaught_and_exit freturnsf (), unless that raises, in which case it prints the exception and exits nonzero.
val reraise_uncaught : string -> (unit -> 'a) -> 'aTraces exceptions passing through. Useful because in practice, backtraces still don't seem to work.
Example:
let rogue_function () = if Random.bool () then failwith "foo" else 3 let traced_function () = Exn.reraise_uncaught "rogue_function" rogue_function traced_function ();;: Program died with Reraised("rogue_function", Failure "foo")