Reputation: 529
Debugging an OCaml program that has an unhandled exception is fairly easy, because the program stops running and you can obtain a backtrace by running in ocamldebug
or setting the OCAMLRUNPARAM
environment variable to b
. Is there any way to obtain such a backtrace for a handled exception?
Note: Modifying the program so it does not handle the exception is of course one option. However, I'd like to avoid modifying the program if possible. Something analogous to gdb
's catch
command would be great.
Upvotes: 1
Views: 314
Reputation: 66818
There is a function Printexc.print_backtrace
that will print a backtrace showing the stack from the point the exception was raised to the current point where the exception is being handled. This might help, though note that it doesn't print a full stack backtrace.
I once wrote some hacky code using Unix.fork
that prints a full stack backtrace on a Unix-like system. See my answer to printing stack traces. (I wouldn't suggest using this code in production.)
Upvotes: 2