在 Clojure 中获取调用堆栈
当我运行 Clojure 程序并在执行过程中出现错误时,我注意到 REPL 打印的消息仅包含我执行的脚本中的顶级行号。我可以让它转储调用堆栈(引用 Clojure 代码的各个行号)吗?
例如:
user=> (load-file "test.clj")
java.lang.IllegalArgumentException: Wrong number of args (1) passed to: user$eval134$fn (test.clj:206)
user=>
如果我知道的不仅仅是顶层调用(第 206 行),那就更好了。
When I run my Clojure programs and get an error during execution, I notice that the message printed by the REPL only contains the top level line number from the script I executed. Can I get it to dump a call stack (which references the various line numbers of Clojure code)?
For example:
user=> (load-file "test.clj")
java.lang.IllegalArgumentException: Wrong number of args (1) passed to: user$eval134$fn (test.clj:206)
user=>
It would be nicer if I knew more than just the top level call (line 206).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后抛出的异常可在
*e
var 中找到。您可以通过在异常上调用.printStackTrace
来打印堆栈跟踪。如果您的异常是由文件中的源代码引发的,它将打印行号;如果来自 REPL,则它将打印 NO_SOURCE_FILE,如下面的示例所示。在 Clojure 1.3 (alpha) 中,有一个名为
pst
的函数可以执行相同的操作。这些堆栈跟踪要好一些,因为删除了一些无关的行。某些 IDE(例如 Emacs 的 SLIME)会自动为您弹出堆栈跟踪。还有一些用于显示和操作堆栈跟踪的库,例如 clojure.stacktrace和 clj-stacktrace。
堆栈跟踪处理似乎是 Clojure 仍在完善的一个方面。
The last Exception thrown is available in the
*e
var. You can print a stack trace by calling.printStackTrace
on the Exception. It'll print line numbers if your Exception was thrown by source code in a file, or NO_SOURCE_FILE if it's from the REPL, like in my examples below.In Clojure 1.3 (alpha) there's a function called
pst
that does the same thing. These stack traces are a bit nicer because some extraneous lines are removed.Certain IDEs (e.g. SLIME for Emacs) will pop up the stack trace for you automatically. There are also some libraries for displaying and manipulating stacktraces, like clojure.stacktrace and clj-stacktrace.
Stack trace handling seems to be an aspect of Clojure that's still being refined.