2

如何使用速度模板打印异常的完整堆栈跟踪

我现在的模板有 $exception 作为模板变量,其中包含异常。

4

2 回答 2

1

在速度上,这就是我解决这个问题的方法。

#foreach ($i in [1..3])     
   #if($异常。原因)
           #set($exception = $exception.Cause)
           #foreach($stack in $exception.getStackTrace())
               $stack.toString()
           #结尾     
    #结尾
#结尾
于 2009-03-10T23:10:05.633 回答
0

如果您想在输出中显示跟踪,您可以将评估方法包装在 try-catch-finally 块中。在 catch 中将堆栈跟踪或仅将异常消息写入编写器。在最后部分,只需刷新作者即可。

如果我没记错的话是这样的:

StringWriter w = new StringWriter();
try {
    Velocity.evaluate( context, w, "mystring", s );
} catch (Exceptions... e)
    w.write(e.getMessage());
} finally {
    w.flush();
}
于 2009-02-06T20:58:33.123 回答