gpt4 book ai didi

ocaml - 我可以让 OCaml 在未捕获的异常上生成堆栈跟踪吗?

转载 作者:行者123 更新时间:2023-12-05 01:47:49 24 4
gpt4 key购买 nike

在 Java 中,当异常从 main() 函数中逃逸时,堆栈跟踪将打印到控制台。你能让 OCaml 程序做同样的事情吗?

最佳答案

是的,用-g编译并设置OCAMLRUNPARM=b

$ cat exc.ml
let f () : int =
raise End_of_file

let g () =
f () + 44

let _ = g()
$ ocamlc -g -o exc exc.ml
$ OCAMLRUNPARAM=b exc
Fatal error: exception End_of_file
Raised at file "exc.ml", line 2, characters 10-21
Called from file "exc.ml", line 5, characters 4-8
Called from file "exc.ml", line 7, characters 8-11

感谢 Daniel Bünzli 指出,如果您编译为 native 代码,行为可能会有所不同。这是我在系统(Mac OS X 10.9.1、OCaml 4.01.0)上看到的:

$ ocamlopt -g -o exc exc.ml
$ OCAMLRUNPARAM=b exc
Fatal error: exception End_of_file
Raised by primitive operation at file "exc.ml", line 5, characters 4-8
Called from file "exc.ml", line 7, characters 8-11

如果关闭内联,一切似乎都运行良好(至少对于这个非常简单的示例而言):

$ ocamlopt -inline 0 -g -o exc exc.ml
$ OCAMLRUNPARAM=b exc
Fatal error: exception End_of_file
Raised at file "exc.ml", line 2, characters 10-21
Called from file "exc.ml", line 5, characters 4-8
Called from file "exc.ml", line 7, characters 8-11

关于ocaml - 我可以让 OCaml 在未捕获的异常上生成堆栈跟踪吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21613715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com