在 Clojure 中获取调用堆栈 [英] Obtain a callstack in Clojure

查看:23
本文介绍了在 Clojure 中获取调用堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行 Clojure 程序并在执行过程中出错时,我注意到 REPL 打印的消息仅包含我执行的脚本中的顶级行号.我可以让它转储调用堆栈(引用 Clojure 代码的各种行号)吗?

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)?

例如:

user=> (load-file "test.clj")
java.lang.IllegalArgumentException: Wrong number of args (1) passed to: user$eval134$fn (test.clj:206)
user=>

如果我知道的不仅仅是顶级调用(第 206 行)会更好.

It would be nicer if I knew more than just the top level call (line 206).

推荐答案

最后抛出的异常可在 *e 变量中找到.您可以通过在异常上调用 .printStackTrace 来打印堆栈跟踪.如果您的异常是由文件中的源代码抛出的,它将打印行号,如果它来自 REPL,则它会打印 NO_SOURCE_FILE,就像我下面的示例一样.

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.

Clojure 1.2.0
user=> (throw (Exception. "FOO"))
java.lang.Exception: FOO (NO_SOURCE_FILE:0)
user=> *e
#<CompilerException java.lang.Exception: FOO (NO_SOURCE_FILE:0)>
user=> (.printStackTrace *e)
java.lang.Exception: FOO (NO_SOURCE_FILE:0)
        at clojure.lang.Compiler.eval(Compiler.java:5440)
        at clojure.lang.Compiler.eval(Compiler.java:5391)
        at clojure.core$eval.invoke(core.clj:2382)
        at clojure.main$repl$read_eval_print__5624.invoke(main.clj:183)
        at clojure.main$repl$fn__5629.invoke(main.clj:204)
        at clojure.main$repl.doInvoke(main.clj:204)
        at clojure.lang.RestFn.invoke(RestFn.java:422)
        at clojure.main$repl_opt.invoke(main.clj:262)
        at clojure.main$main.doInvoke(main.clj:355)
        at clojure.lang.RestFn.invoke(RestFn.java:398)
        at clojure.lang.Var.invoke(Var.java:361)
        at clojure.lang.AFn.applyToHelper(AFn.java:159)
        at clojure.lang.Var.applyTo(Var.java:482)
        at clojure.main.main(main.java:37)
Caused by: java.lang.Exception: FOO
        at user$eval1.invoke(NO_SOURCE_FILE:1)
        at clojure.lang.Compiler.eval(Compiler.java:5424)
        ... 13 more
nil

在 Clojure 1.3 (alpha) 中,有一个名为 pst 的函数可以做同样的事情.这些堆栈跟踪更好一些,因为删除了一些无关的行.

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.

Clojure 1.3.0-master-SNAPSHOT
user=> (throw (Exception. "FOO"))
Exception FOO  user/eval1 (NO_SOURCE_FILE:1)
user=> (pst)
Exception FOO
        user/eval1 (NO_SOURCE_FILE:1)
        clojure.lang.Compiler.eval (Compiler.java:5998)
        clojure.lang.Compiler.eval (Compiler.java:5965)
        clojure.core/eval (core.clj:2652)
        clojure.core/eval (core.clj:-1)
        clojure.main/repl/read-eval-print--5575 (main.clj:178)
        clojure.main/repl/fn--5580 (main.clj:199)
        clojure.main/repl (main.clj:199)
        clojure.main/repl-opt (main.clj:257)
        clojure.main/main (main.clj:350)
        clojure.lang.Var.invoke (Var.java:361)
        clojure.lang.Var.applyTo (Var.java:482)
nil

某些 IDE(例如 Emacs 的 SLIME)会自动为您弹出堆栈跟踪.还有一些用于显示和操作堆栈跟踪的库,例如 clojure.stacktraceclj-stacktrace.

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.

堆栈跟踪处理似乎是 Clojure 的一个仍在完善中的方面.

Stack trace handling seems to be an aspect of Clojure that's still being refined.

这篇关于在 Clojure 中获取调用堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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