如何获取e.printStackTrace()的内容

时间:2022-10-21 09:05:34
package app;
import java.io.PrintWriter;
import java.io.StringWriter;
public class E {
public static void main(String[] args) {
String str = null;
try {
System.out.println(str.charAt(0));
//System.out.println(1/0);
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println(e.fillInStackTrace().getMessage());
e.printStackTrace();
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw, true));
String strs = sw.toString();
System.out.println("--------------------------美丽的分割线----------------------");
System.out.println(strs);
}
}

}

打印的效果如下:

null
null
java.lang.NullPointerException
at app.E.main(E.java:14)
--------------------------美丽的分割线----------------------
java.lang.NullPointerException
at app.E.main(E.java:14)