(stack trace)异常栈轨迹是指:
当 throw Throwable 时, Throwable对象抛出过程(穿梭)所经历的多个方法调用层(方法调用栈)。越接近 throw 语句的方法先进入异常栈。this
(Throwable Causal Chain)异常缘由链:
在Throwable类中的cause属性,表示被当前异常包装的原始异常。(能够称为异常缘由)
在打印异常栈轨迹时,会递归打印 原始异常的异常栈。code
如今来分析一下 Throwable.printStackTrace() 方法。
在
Throwable.printStackTrace(PrintStreamOrWriter s) 内部,咱们能够看到:
在打印异常栈时,是对象
synchronized (s.lock()) { // Print our stack trace // 1. 先打印 当前异常对象this的 异常栈。 s.println(this); StackTraceElement[] trace = getOurStackTrace(); for (StackTraceElement traceElement : trace) s.println("\tat " + traceElement); // Print suppressed exceptions, if any for (Throwable se : getSuppressed()) se.printEnclosedStackTrace(s, trace, SUPPRESSED_CAPTION, "\t", dejaVu); // Print cause, if any // 2. 打印 异常缘由链 Throwable ourCause = getCause(); if (ourCause != null) ourCause.printEnclosedStackTrace(s, trace, CAUSE_CAPTION, "", dejaVu); }