我可以让OCaml在未捕获的异常上生成堆栈跟踪吗? [英] Can I make OCaml produce stack traces on uncaught exceptions?

查看:60
本文介绍了我可以让OCaml在未捕获的异常上生成堆栈跟踪吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,当异常转义main()函数时,会将堆栈跟踪打印到控制台.您可以让OCaml程序做同样的事情吗?

In Java when an exception escapes the main() function, a stacktrace is printed to the console. Can you make OCaml programs do the same thing?

推荐答案

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

Yes, compile with -g and set 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

感谢DanielBünzli指出,如果编译为本地代码,其行为可能会有所不同.这是我在系统(Mac OS X 10.9.1,OCaml 4.01.0)上看到的内容:

Thanks to Daniel Bünzli for pointing out that the behavior can be different if you compile to native code. Here's what I see on my system (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

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

If you turn off inlining, things seem to work pretty well (at least for this very simple example):

$ 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在未捕获的异常上生成堆栈跟踪吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆