views:

599

answers:

2

How to print the complete stack trace of the exception using a velocity template

My present template has $exception as template variable, which contains the exception.

A: 

In case you want to display the trace on the output you could wrap the evaluate method inside try-catch-finally block. In catch write the stack trace or just the message of the exception to the writer. In the finally section just flush the writer.

Something like this if I remember right:

StringWriter w = new StringWriter();
try {
    Velocity.evaluate( context, w, "mystring", s );
} catch (Exceptions... e)
    w.write(e.getMessage());
} finally {
    w.flush();
}
Petteri Hietavirta
I wanted to show the exception on the html page which will be enabled in testing development mode. However in production mode, the switch automatically doesn't show the error.This solution doesn't work for my situation.
Spring Monkey
A: 

In velocity this is how i solved this problem.

#foreach ($i in [1..3])     
   #if($exception.Cause)
           #set($exception = $exception.Cause)
           #foreach($stack in $exception.getStackTrace())
            $stack.toString()
           #end     
    #end
#end
Spring Monkey