Printing stack traces

后端 未结 2 1923
谎友^
谎友^ 2021-01-20 02:04

I have a very short test file:

let print_backtrace () = try raise Not_found with
    Not_found -> Printexc.print_backtrace stdout;;

let f () = print_back         


        
2条回答
  •  花落未央
    2021-01-20 02:23

    The documentation for Printexc.print_backtrace says:

    The backtrace lists the program locations where the most-recently raised exception was raised and where it was propagated through function calls.

    It actually seems to be doing the right thing. The exception hasn't been propagated back through f.

    If I move the call to Printexc.print_backtrace outside the call to f, I see a full backtrace.

    $ cat test2.ml
    let print_backtrace () = raise Not_found
    
    let f () = let res = print_backtrace () in res ;;
    
    try f () with Not_found -> Printexc.print_backtrace stdout
    $ /usr/local/ocaml312/bin/ocamlc -g test2.ml
    $ OCAMLRUNPARAM=b a.out 
    Raised at file "test2.ml", line 1, characters 31-40
    Called from file "test2.ml", line 3, characters 21-39
    Called from file "test2.ml", line 5, characters 4-8
    

提交回复
热议问题