关于java:如何将printStackTrace存储到字符串中

How to store printStackTrace into a string

本问题已经有最佳答案,请猛点这里访问。

如何获取e.printStackTrace()并将其存储到String变量中?我想在以后的程序中使用由e.printStackTrace()生成的字符串。

我对Java仍然陌生,所以我不太熟悉EDOCX1,3,我认为将是解决方案。或者如果你有其他想法,请告诉我。谢谢


有点像

1
2
3
StringWriter errors = new StringWriter();
ex.printStackTrace(new PrintWriter(errors));
return errors.toString();

应该是你需要的。

相关文件:

  • 编剧
  • 字符打印流
  • 可抛出


使用throwables.getstacktraceasstring(throwable)可以轻松完成此操作:

1
2
Exception e = ...
String stackTrace = Throwables.getStackTraceAsString(e);

在内部,这是@zach l建议的。


您可以使用ApacheCommons3类org.apache.commons.lang3.exception.ExceptionUtils中的ExceptionUtils.getStackTrace(Throwable t);

http://commons.apache.org/proper/commons-lang/

exceptionutils.getstacktrace(throwable T)

代码示例:

1
2
3
4
5
6
7
try {

  // your code here

} catch(Exception e) {
  String s = ExceptionUtils.getStackTrace(e);
}


沿着瓜娃的路线,阿帕奇公地语言在埃多克斯1〔3〕有埃多克斯1〔2〕。从StackOverflow的先前答案。


您必须使用getStackTrace ()方法而不是printStackTrace()方法。下面是一个很好的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.*;

/**
* Simple utilities to return the stack trace of an
* exception as a String.
*/

public final class StackTraceUtil {

  public static String getStackTrace(Throwable aThrowable) {
    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    aThrowable.printStackTrace(printWriter);
    return result.toString();
  }

  /**
  * Defines a custom format for the stack trace as String.
  */

  public static String getCustomStackTrace(Throwable aThrowable) {
    //add the class name and any message passed to constructor
    final StringBuilder result = new StringBuilder("BOO-BOO:" );
    result.append(aThrowable.toString());
    final String NEW_LINE = System.getProperty("line.separator");
    result.append(NEW_LINE);

    //add each element of the stack trace
    for (StackTraceElement element : aThrowable.getStackTrace() ){
      result.append( element );
      result.append( NEW_LINE );
    }
    return result.toString();
  }

  /** Demonstrate output.  */
  public static void main (String... aArguments){
    final Throwable throwable = new IllegalArgumentException("Blah");
    System.out.println( getStackTrace(throwable) );
    System.out.println( getCustomStackTrace(throwable) );
  }
}


1
2
3
4
5
6
StackTraceElement[] stack = new Exception().getStackTrace();
String theTrace ="";
for(StackTraceElement line : stack)
{
   theTrace += line.toString();
}


使用apache commons-lang3 lib

1
2
3
4
5
6
import org.apache.commons.lang3.exception.ExceptionUtils;

//...

String[] ss = ExceptionUtils.getRootCauseStackTrace(e);
logger.error(StringUtils.join(ss, System.lineSeparator()));


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
call:  getStackTraceAsString(sqlEx)

public String getStackTraceAsString(Exception exc)  
{  
String stackTrace ="*** Error in getStackTraceAsString()";

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream( baos );
exc.printStackTrace(ps);
try {
    stackTrace = baos.toString("UTF8" ); // charsetName e.g. ISO-8859-1
    }
catch( UnsupportedEncodingException ex )
    {
    Logger.getLogger(sss.class.getName()).log(Level.SEVERE, null, ex);
    }
ps.close();
try {
    baos.close();
    }
catch( IOException ex )
    {
    Logger.getLogger(sss.class.getName()).log(Level.SEVERE, null, ex);
    }
return stackTrace;
}