【问题标题】:How to print stack trace of an exception in a velocity template如何在速度模板中打印异常的堆栈跟踪
【发布时间】:2009-02-06 19:56:32
【问题描述】:

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

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

【问题讨论】:

    标签: exception stack-trace velocity


    【解决方案1】:

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

    #foreach ($i in [1..3]) #if($异常。原因) #set($exception = $exception.Cause) #foreach($stack in $exception.getStackTrace()) $stack.toString() #结尾 #结尾 #结尾

    【讨论】:

      【解决方案2】:

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

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

      StringWriter w = new StringWriter();
      try {
          Velocity.evaluate( context, w, "mystring", s );
      } catch (Exceptions... e)
          w.write(e.getMessage());
      } finally {
          w.flush();
      }
      

      【讨论】:

      • 我想在测试开发模式下启用的 html 页面上显示异常。但是在生产模式下,开关自动不显示错误。此解决方案不适用于我的情况。
      猜你喜欢
      • 1970-01-01
      • 2014-08-06
      • 2012-09-04
      • 1970-01-01
      • 2011-05-15
      • 2011-01-18
      • 2012-01-03
      • 2010-09-25
      • 1970-01-01
      相关资源
      最近更新 更多