Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Finding how and where an exception is raised
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-07-19 (13:47)
From: David Mentre <David.Mentre@i...>
Subject: [Caml-list] Finding how and where an exception is raised

I would like to find how and where an exception is raised by printing a
stack backtrace (something like Java Throwable.printStackTrace()).

It seems to be impossible in plain ocaml. I could set the environment
variable OCAMLRUNPARAM="b=1" and let the exception escape the program,
but I would only get one exception.

Right now, I'm using the 'assert false' trick[1]. However, I'm forced to
put the assert at the place the test is done and I can't embed it in an
inner function (something like in [2]).

Has anybody a better solution?

Best regards,


[2] In the following program, the exception raised in meaningless
(always in the check function):

let check b =
  if b then (print_char '.'; flush stdout) 
  else (print_char 'F'; flush stdout; assert(false))

let run () =
  check (true = true); 
  check (true != false); 
  check ("a" != "b")

let _ = 
    ~f:(fun (s, f) -> 
        print_string s; 
        f (); 
        print_newline ()
      with Assert_failure (filename, first, last) ->
          "\nCheck failed in file '%s', expression at characters %d-%d\n"
          filename first last;
        flush stdout)
    [ ("support", run) ]

 Opinions expressed here are only mine.
Bug reports:  FAQ:
To unsubscribe, mail  Archives: