7

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!

2
  • See stackoverflow.com/questions/944991/… Commented May 10, 2012 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. Commented May 10, 2012 at 19:00

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

BTW: pst print stack trace to *err*
2

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

(.getStackTrace (Thread/currentThread))

1 Comment

This seems to be the way if you want print the trace outside repl. Printing needed bit work. (doseq [element (.getStackTrace (Thread/currentThread))](clojure.stacktrace/print-trace-element element) (println))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.