【问题标题】:Get stacktrace of function with throws Exception [duplicate]使用 throws Exception 获取函数的堆栈跟踪 [重复]
【发布时间】:2017-03-11 20:45:55
【问题描述】:

假设我有这样的功能

public JSONObject parseMessage(InputStream fileName) throws Exception { 

它会抛出一个错误,我在哪里可以得到它的堆栈跟踪?

我知道我是否有类似的东西

try {} catch(Exception e) {//I could save it to a var here}

但是如何使用throws Exception 的函数将堆栈跟踪保存为变量?

感谢大家的帮助

【问题讨论】:

  • 您想在函数抛出异常之前获取堆栈跟踪信息吗?
  • 你可以catch它,处理它,然后throw它再次。

标签: java exception try-catch stack-trace


【解决方案1】:

您可以使用Apache commons 将异常堆栈跟踪转换为字符串。

这个类在 Apache commons-lang-2.2.jar 中可用:

org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(Throwable)

【讨论】:

  • 或者只使用现有的.getStackTrace(),它返回StackTraceElement[],它有一个非常好的.toString()
  • Arrays.toString(Thread.currentThread().getStackTrace());
猜你喜欢
  • 2010-10-11
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 2011-10-01
  • 2021-08-21
  • 2016-11-06
  • 2011-01-22
  • 1970-01-01
相关资源
最近更新 更多