exceptionclojurestack-traceprintstacktrace

Get stacktrace as string


I'm using Clojure and I want to get my hands on a stack trace that I can log (ideally, i would like to get it as a String).

I see that (.getStackTrace e) returns a StackTraceElement[] but I don't know how to print something meaningful out of it. My second approach was (.printStackTrace e) with a PrintWriter as a parameter (because I know this is possible in Java), but I don't seem to get the correct syntax.

Thanks.


Solution

  • if number23_cn's solution is a bit much, this is how you can use the result of .getStackTrace as a string (which can then be printed, put in a log, whatever)

    (try (/ 1 0)
      (catch Throwable t
        (map str (.getStackTrace t))))