Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
OCaml handler for uncaught exception #5941
Original bug ID: 5941
The attached patch allows the user to register an OCaml function to handle uncaught exceptions. The default handler does the same as the C function except that it calls [Printexc.to_string], which uses user-defined exception printers.
The new function is:
Printexc.set_uncaught_exception_handler: (exn -> raw_backtrace option -> unit) -> unit
The backtrace is [None] when the debugger is active.
Comment author: @gasche
I'm a bit confused by your caml_callback2 invokation: the second parameter is Val_bool(DEBUGGER_IN_USE), while the OCaml type expects an argument of type (raw_backtrace option).
Otherwise I'm impressed by the speed at which you made use of the new raw_backtrace type :-)
Could you explain what the debugger test is there for? I am not familiar with its interaction with the backtrace code.
When developing the raw_backtrace interface, Jacques-Henri Jourdan and I wondered if we should expose whether the backtrace was correctly recorded or not directly in Printexc.get_raw_backtrace, by having it return a
Comment author: @diml
caml_callback2 is applied on Printexc.handle_uncaught_exception, which is a wrapper around the user provided function.
I'm not 100% sure, but looking at startup.c the debugger interactive loop is called before caml_fatal_uncaught_exception, so that maybe related (the test was already there). Actually I don't think the latter will ever be called when using ocamldebug.
I used an option because of this debugger test, but I should probably follow the same convention as for get_raw_backtrace. I have no strong opinion about whether it should be an option or not, but if get_raw_backtrace cannot be made to return None exactly when the backtrace is invalid I think it is better to just return an empty one.