Java 必知必会 第 23 篇
how-can-i-convert-a-stack-trace-to-a-string

怎样将堆栈追踪信息转换为字符串

问题

Throwable.getStackTrace()的结果转换为一个字符串来来描述堆栈信息的最简单的方法是什么

最佳答案

可以用下面的方法将异常堆栈信息转换为字符串类型。该类在Apache commons-lang-2.2.jar中可以找到:
org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(Throwable)

答案二

Throwable.printStackTrace(PrintWriter pw)可以输出堆栈信息:

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
sw.toString(); // stack trace as a string

答案三

StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();

答案四

public String stackTraceToString(Throwable e) {
    StringBuilder sb = new StringBuilder();
    for (StackTraceElement element : e.getStackTrace()) {
        sb.append(element.toString());
        sb.append("\n");
    }
    return sb.toString();
}