【问题标题】:How to restrict lines of code in exception printStackTrace如何限制异常 printStackTrace 中的代码行
【发布时间】:2016-07-23 21:42:25
【问题描述】:
try
{
 //logic
}
catch(Exception e)
{
      e.printStackTrace();
}

printStackTrace() 方法将为我们提供与异常相关的语句行,但其中所有行对我们都没有用

我们如何限制异常细节行

【问题讨论】:

  • 你想要错误信息....改用e.getMessage()
  • Java 应该如何知道堆栈跟踪中的哪些类与您相关?如果您想手动检查/打印堆栈跟踪,请使用 e.getStackTrace()

标签: java exception printstacktrace


【解决方案1】:

您可以将堆栈跟踪写入 PrintWriter,然后获取写入器的输出,并限制字节数或行数,或使用正则表达式对其进行过滤,或任何适合您的需要。

} catch (Exception e) {
    StringWriter sw = new StringWriter();
    e.printStackTrace(new PrintWriter(sw));
    String fullStackTrace = sw.toString();
    String restrictedStackTrace = ... //now you can manipulate the fullStackTrace
    System.err.print(restrictedStackTrace);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多