6

I am aware of http://richhickey.github.com/clojure/clojure.stacktrace-api.html .

Is there a way to get the current stacktrace w/o throwing an exception and catching it?

(I'm debugging a piece of code, and want to capture stacktraces at certain points so I can analyze what's going on.)

Thanks!

user1383359
  • 2,673
  • 2
  • 25
  • 32
  • See http://stackoverflow.com/questions/944991/is-there-a-way-to-dump-a-stack-trace-without-throwing-an-exception-in-java – user100464 May 10 '12 at 11:11
  • So basically: either create your own exception, or call a function which creates its own exception? I didn't realize exceptions were such low level / inexpensive primitives. I thought surely there must be some overheat that can be eliminated if I just want the stacktrace. – user1383359 May 10 '12 at 19:00

2 Answers2

8

use clojure.repl.pst

user=> (try (/ 1 0) (catch Exception e (pst e)))
ArithmeticException Divide by zero
    clojure.lang.Numbers.divide (Numbers.java:156)
    clojure.lang.Numbers.divide (Numbers.java:3691)
    user/eval28 (NO_SOURCE_FILE:8)
    clojure.lang.Compiler.eval (Compiler.java:6511)
    clojure.lang.Compiler.eval (Compiler.java:6477)
    clojure.core/eval (core.clj:2797)
    clojure.main/repl/read-eval-print--6569 (main.clj:245)
    clojure.main/repl/fn--6574 (main.clj:266)
    clojure.main/repl (main.clj:266)
    clojure.main/repl-opt (main.clj:332)
    clojure.main/main (main.clj:427)
    clojure.lang.Var.invoke (Var.java:423)
number23_cn
  • 4,611
  • 26
  • 31
1

This code return StackTraceElement array which is not so hard to turn in readable form:

(.getStackTrace (Thread/currentThread))
Community
  • 1
  • 1
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228