Callstack from load-file

1 view
Skip to first unread message

jim

unread,
Oct 18, 2008, 12:40:52 PM10/18/08
to Clojure
Hey all,

I had a situation where a source file wouldn't load and the error
message was unhelpful:

java.lang.IllegalArgumentException: Too many arguments to struct
constructor (source-file.clj:0)

So, I threw together a quick hack to print the call stack:

(defn callstack-str [thrown]
(let [stk (new java.io.StringWriter)
pw (new java.io.PrintWriter stk)]
(. thrown (printStackTrace pw))
(str (. stk (toString)))))

(defn lf [filename]
(try
(load-file filename)
(catch java.lang.Throwable e
(print (callstack-str e)))))

As usual, I'm sure there's a better way, so I'd appreciate hearing
about it.

Jim

Parth Malwankar

unread,
Oct 18, 2008, 1:27:53 PM10/18/08
to Clojure
Hi Jim,

The default behavior of the REPL is to show only the
exception to avoid too much clutter on the screen.
The special variable *e stores the last exception which
can be used to see the stack trace. So one way to do
this would be:

user=> (defn foo [] (/ 1 0))
#=(var user/foo)

user=> (foo)
java.lang.ArithmeticException: Divide by zero (NO_SOURCE_FILE:0)

user=> *e
#=(clojure.lang.Compiler$CompilerException.
"java.lang.ArithmeticException: Divide by zero (NO_SOURCE_FILE:0)")

user=> (.printStackTrace *e)
java.lang.ArithmeticException: Divide by zero (NO_SOURCE_FILE:0)
at clojure.lang.Compiler.eval(Compiler.java:4117)
at clojure.lang.Repl.main(Repl.java:87)
Caused by: java.lang.ArithmeticException: Divide by zero
at clojure.lang.Numbers.divide(Numbers.java:142)
at user.foo__2478.invoke(Unknown Source)
at user.eval__2481.invoke(Unknown Source)
at clojure.lang.Compiler.eval(Compiler.java:4106)
... 1 more
nil
user=>

Parth
Reply all
Reply to author
Forward
0 new messages