@@ -710,12 +710,18 @@ struct
710710end
711711open Basic_helpers
712712
713-
714- (* Small helper function to avoid catching ocaml-runtime exceptions *)
715- let is_not_ocaml_runtime_exception = function
713+ (* Small helpers to avoid catching ocaml-runtime exceptions *)
714+ type exception_filter = exn -> bool
715+ let catch_all_filter = fun _ -> true
716+ let catch_not_runtime_filter = function
716717 | Out_of_memory -> false
717718 | Stack_overflow -> false
718719 | _ -> true
720+ let exception_filter =
721+ (* Default value: the legacy behaviour to avoid breaking programs *)
722+ ref catch_all_filter
723+ let set_exception_filter f = exception_filter := f
724+ let filter_exception e = ! exception_filter e
719725
720726module Sequence_associated_storage :
721727sig
@@ -796,7 +802,7 @@ struct
796802 let result = f () in
797803 current_storage := saved_storage;
798804 result
799- with exn when is_not_ocaml_runtime_exception exn ->
805+ with exn when ! exception_filter exn ->
800806 current_storage := saved_storage;
801807 raise exn
802808end
@@ -1134,7 +1140,7 @@ struct
11341140 be reject later, it is not the responsibility of this function to pass
11351141 the exception to [!async_exception_hook]. *)
11361142 try f v
1137- with exn when is_not_ocaml_runtime_exception exn ->
1143+ with exn when ! exception_filter exn ->
11381144 ! async_exception_hook exn
11391145
11401146
@@ -1834,7 +1840,7 @@ struct
18341840
18351841 let p' =
18361842 try f v with exn
1837- when is_not_ocaml_runtime_exception exn -> fail exn
1843+ when ! exception_filter exn -> fail exn
18381844 in
18391845 let Internal p' = to_internal_promise p' in
18401846 (* Run the user's function [f]. *)
@@ -1900,7 +1906,7 @@ struct
19001906
19011907 let p' =
19021908 try f v
1903- with exn when is_not_ocaml_runtime_exception exn ->
1909+ with exn when ! exception_filter exn ->
19041910 fail (add_loc exn ) in
19051911 let Internal p' = to_internal_promise p' in
19061912
@@ -1957,7 +1963,7 @@ struct
19571963
19581964 let p''_result =
19591965 try Fulfilled (f v) with exn
1960- when is_not_ocaml_runtime_exception exn -> Rejected exn
1966+ when ! exception_filter exn -> Rejected exn
19611967 in
19621968
19631969 let State_may_now_be_pending_proxy p'' = may_now_be_proxy p'' in
@@ -1987,7 +1993,7 @@ struct
19871993 to_public_promise
19881994 {state =
19891995 try Fulfilled (f v)
1990- with exn when is_not_ocaml_runtime_exception exn -> Rejected exn })
1996+ with exn when ! exception_filter exn -> Rejected exn })
19911997 ~if_deferred: (fun () ->
19921998 let (p'', callback) =
19931999 create_result_promise_and_callback_if_deferred () in
@@ -2006,7 +2012,7 @@ struct
20062012 let catch f h =
20072013 let p =
20082014 try f ()
2009- with exn when is_not_ocaml_runtime_exception exn -> fail exn
2015+ with exn when ! exception_filter exn -> fail exn
20102016 in
20112017 let Internal p = to_internal_promise p in
20122018 let p = underlying p in
@@ -2031,7 +2037,7 @@ struct
20312037
20322038 let p' =
20332039 try h exn
2034- with exn when is_not_ocaml_runtime_exception exn -> fail exn
2040+ with exn when ! exception_filter exn -> fail exn
20352041 in
20362042 let Internal p' = to_internal_promise p' in
20372043
@@ -2067,7 +2073,7 @@ struct
20672073 let backtrace_catch add_loc f h =
20682074 let p =
20692075 try f ()
2070- with exn when is_not_ocaml_runtime_exception exn -> fail exn
2076+ with exn when ! exception_filter exn -> fail exn
20712077 in
20722078 let Internal p = to_internal_promise p in
20732079 let p = underlying p in
@@ -2092,7 +2098,7 @@ struct
20922098
20932099 let p' =
20942100 try h exn
2095- with exn when is_not_ocaml_runtime_exception exn ->
2101+ with exn when ! exception_filter exn ->
20962102 fail (add_loc exn )
20972103 in
20982104 let Internal p' = to_internal_promise p' in
@@ -2129,7 +2135,7 @@ struct
21292135 let try_bind f f' h =
21302136 let p =
21312137 try f ()
2132- with exn when is_not_ocaml_runtime_exception exn -> fail exn
2138+ with exn when ! exception_filter exn -> fail exn
21332139 in
21342140 let Internal p = to_internal_promise p in
21352141 let p = underlying p in
@@ -2146,7 +2152,7 @@ struct
21462152
21472153 let p' =
21482154 try f' v
2149- with exn when is_not_ocaml_runtime_exception exn -> fail exn
2155+ with exn when ! exception_filter exn -> fail exn
21502156 in
21512157 let Internal p' = to_internal_promise p' in
21522158
@@ -2162,7 +2168,7 @@ struct
21622168
21632169 let p' =
21642170 try h exn
2165- with exn when is_not_ocaml_runtime_exception exn -> fail exn
2171+ with exn when ! exception_filter exn -> fail exn
21662172 in
21672173 let Internal p' = to_internal_promise p' in
21682174
@@ -2204,7 +2210,7 @@ struct
22042210 let backtrace_try_bind add_loc f f' h =
22052211 let p =
22062212 try f ()
2207- with exn when is_not_ocaml_runtime_exception exn -> fail exn
2213+ with exn when ! exception_filter exn -> fail exn
22082214 in
22092215 let Internal p = to_internal_promise p in
22102216 let p = underlying p in
@@ -2221,7 +2227,7 @@ struct
22212227
22222228 let p' =
22232229 try f' v
2224- with exn when is_not_ocaml_runtime_exception exn ->
2230+ with exn when ! exception_filter exn ->
22252231 fail (add_loc exn )
22262232 in
22272233 let Internal p' = to_internal_promise p' in
@@ -2238,7 +2244,7 @@ struct
22382244
22392245 let p' =
22402246 try h exn
2241- with exn when is_not_ocaml_runtime_exception exn ->
2247+ with exn when ! exception_filter exn ->
22422248 fail (add_loc exn )
22432249 in
22442250 let Internal p' = to_internal_promise p' in
@@ -2493,7 +2499,7 @@ struct
24932499 let dont_wait f h =
24942500 let p =
24952501 try f ()
2496- with exn when is_not_ocaml_runtime_exception exn -> fail exn
2502+ with exn when ! exception_filter exn -> fail exn
24972503 in
24982504 let Internal p = to_internal_promise p in
24992505
@@ -2516,7 +2522,7 @@ struct
25162522 let async f =
25172523 let p =
25182524 try f ()
2519- with exn when is_not_ocaml_runtime_exception exn -> fail exn
2525+ with exn when ! exception_filter exn -> fail exn
25202526 in
25212527 let Internal p = to_internal_promise p in
25222528
@@ -3119,39 +3125,39 @@ struct
31193125
31203126
31213127 let apply f x =
3122- try f x with exn when is_not_ocaml_runtime_exception exn -> fail exn
3128+ try f x with exn when ! exception_filter exn -> fail exn
31233129
31243130 let wrap f =
31253131 try return (f () )
3126- with exn when is_not_ocaml_runtime_exception exn -> fail exn
3132+ with exn when ! exception_filter exn -> fail exn
31273133
31283134 let wrap1 f x1 =
31293135 try return (f x1)
3130- with exn when is_not_ocaml_runtime_exception exn -> fail exn
3136+ with exn when ! exception_filter exn -> fail exn
31313137
31323138 let wrap2 f x1 x2 =
31333139 try return (f x1 x2)
3134- with exn when is_not_ocaml_runtime_exception exn -> fail exn
3140+ with exn when ! exception_filter exn -> fail exn
31353141
31363142 let wrap3 f x1 x2 x3 =
31373143 try return (f x1 x2 x3)
3138- with exn when is_not_ocaml_runtime_exception exn -> fail exn
3144+ with exn when ! exception_filter exn -> fail exn
31393145
31403146 let wrap4 f x1 x2 x3 x4 =
31413147 try return (f x1 x2 x3 x4)
3142- with exn when is_not_ocaml_runtime_exception exn -> fail exn
3148+ with exn when ! exception_filter exn -> fail exn
31433149
31443150 let wrap5 f x1 x2 x3 x4 x5 =
31453151 try return (f x1 x2 x3 x4 x5)
3146- with exn when is_not_ocaml_runtime_exception exn -> fail exn
3152+ with exn when ! exception_filter exn -> fail exn
31473153
31483154 let wrap6 f x1 x2 x3 x4 x5 x6 =
31493155 try return (f x1 x2 x3 x4 x5 x6)
3150- with exn when is_not_ocaml_runtime_exception exn -> fail exn
3156+ with exn when ! exception_filter exn -> fail exn
31513157
31523158 let wrap7 f x1 x2 x3 x4 x5 x6 x7 =
31533159 try return (f x1 x2 x3 x4 x5 x6 x7)
3154- with exn when is_not_ocaml_runtime_exception exn -> fail exn
3160+ with exn when ! exception_filter exn -> fail exn
31553161
31563162
31573163
0 commit comments