English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

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,

[1] http://caml.inria.fr/archives/200010/msg00225.html

[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: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr