1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module Stdlib = struct
let failwithf fmt = Printf.kprintf failwith fmt
let invalid_argf fmt = Printf.kprintf invalid_arg fmt
let raisef f fmt = Printf.kprintf (fun s -> raise (f s)) fmt
exception Finally of exn * exn
let protect f v ~(finally : 'a -> unit) =
let res =
try f v
with exn ->
(try finally v with final_exn -> raise (Finally (exn, final_exn)));
raise exn
in
finally v;
res
let protect_with f v ~finally =
let res =
try f v
with exn ->
(try ignore @@ finally v with final_exn -> raise (Finally (exn, final_exn)));
raise exn
in
res, finally v
let catch f v = try Ok (f v) with e -> Error (`Exn e)
let try_ignore f v = try f v with _ -> ()
let try_or f g v = try f v with _ -> g v
let try_bool f v = try ignore @@ f v; true with _ -> false
let protect_ f = protect f ()
let protect_with_ f = protect_with f ()
let catch_ f = catch f ()
let try_ignore_ f = try_ignore f ()
let try_or_ f g = try_or f g ()
let try_bool_ f = try_bool f ()
let tee f v ~handler = try f v with e -> handler e; raise e
type 'a return = { return : 'jump . 'a -> 'jump }
let with_return (type a) f : a =
let module M = struct
exception Return of a
end in
try f { return = fun a -> raise (M.Return a) } with M.Return a -> a
end
include Stdlib
let to_string = Printexc.to_string
let format ppf t = Format.pp_print_string ppf (Printexc.to_string t)
let print_backtrace = Printexc.print_backtrace
let get_backtrace = Printexc.get_backtrace
let register_printer = Printexc.register_printer