java,Exception,String

package test;


import java.io.PrintWriter;
import java.io.StringWriter;


public class ExceptionTest {
	public static void main(String[] args) {
		Exception e = new Exception("aaa", new Exception("bbb", new Exception(
				"ccc", new Exception("ddd"))));
//		Throwable t = e;
//		while (t != null) {
//			System.out.println(t.getMessage());
//			t = t.getCause();
//		}
		e.printStackTrace();
		System.out.println(getStackTrace(e));
	}


	public static String getStackTrace(Exception e) {
		StringWriter writer = new StringWriter();
		e.printStackTrace(new PrintWriter(writer, true));
		return writer.toString();
	}
}


你可能感兴趣的:(java,Exception,String)