On Oct 18, 9:40 pm, jim <[EMAIL PROTECTED]> wrote:
> 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

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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to