java代码获取完整的Exception异常信息
下面的java代码可以获取完整的Exception异常信息:
import java.io.PrintWriter; import java.io.StringWriter; public class ExceptionTest { public static void main(String[] args) { try { String aa = ""; System.out.println(aa.substring(3)); } catch (Exception e) { e.printStackTrace(); StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw, true)); String str = sw.toString(); System.out.println("=========="); System.out.println(str); } } }
控制台打印的信息:
java.lang.StringIndexOutOfBoundsException: String index out of range: -3 at java.lang.String.substring(String.java:1875) at ExceptionTest.main(ExceptionTest.java:8) ========== java.lang.StringIndexOutOfBoundsException: String index out of range: -3 at java.lang.String.substring(String.java:1875) at ExceptionTest.main(ExceptionTest.java:8)
e.getmessage < e.tostring < e.getStackTrace +e.tostring = e.printStackTrace