将异常的堆栈跟踪写入字符串
import java.io.StringWriter; import java.io.PrintWriter; /**来自 时代Java公众号 - N o w J a v a . c o m**/ public class Main { public static void main(String[] args) { try { m1(); } catch(MyException e) { String str = getStackTrace(e); // Print the stack trace to the standard output System.out.println(str); } } public static void m1() throws MyException { m2();/* 来 自 nowjava*/ } public static void m2() throws MyException { throw new MyException("Some error has occurred."); } public static String getStackTrace(Throwable e) { StringWriter strWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(strWriter); e.printStackTrace(printWriter); // Get the stack trace as a string String str = strWriter.toString(); return str; } } class MyException extends Exception { public MyException() { super(); } public MyException(String messa