一.java異常類結構

常用的CheckedException
常用的RuntimeException
二.Throwable類源碼
- StackTraceElement。一個final類,代表棧軌迹中的元素,一個異常可能有多個元素。 Java代碼
Throwable學習 - public final class StackTraceElement implements java.io.Serializable {
- // Normally initialized by VM (public constructor added in 1.5)
- private String declaringClass;//com.jyz.study.jdk.exception.ExceptionTest
- private String methodName;//equals
- private String fileName;//ExceptionTest.java
- private int lineNumber;//17
- //可以看到 native方法lineNumber=-2
- public boolean isNativeMethod() {
- return lineNumber == -2;
- }
- //com.jyz.study.jdk.exception.ExceptionTest.equals(ExceptionTest.java:17)
- public String toString() {
- return getClassName() + "." + methodName +
- (isNativeMethod() ? "(Native Method)" :
- (fileName != null && lineNumber >= 0 ?
- "(" + fileName + ":" + lineNumber + ")" :
- (fileName != null ? "("+fileName+")" : "(Unknown Source)")));
- }
- }
- Throwable。java裡所有錯誤和異常的基類。 Java代碼
Throwable學習 - public class Throwable implements Serializable {
- //異常詳細資訊
- private String detailMessage;
- //初始化異常原因case為本身
- private Throwable cause = this;
- //棧軌迹中的元素
- private StackTraceElement[] stackTrace;
- //一般也就四個構造函數
- public Throwable() {
- fillInStackTrace();
- }
- public Throwable(String message) {
- fillInStackTrace();
- detailMessage = message;
- }
- public Throwable(String message, Throwable cause) {
- fillInStackTrace();
- detailMessage = message;
- this.cause = cause;
- }
- public Throwable(Throwable cause) {
- fillInStackTrace();
- detailMessage = (cause==null ? null : cause.toString());
- this.cause = cause;
- }
- //自定義異常時 可以重寫此方法
- public String getMessage() {
- return detailMessage;
- }
- //也可以重寫此方法,比喻綁定國際化資源
- public String getLocalizedMessage() {
- return getMessage();
- }
- //if cause == null return null
- public Throwable getCause() {
- return (cause==this ? null : cause);
- }
- //構造異常鍊
- public synchronized Throwable initCause(Throwable cause) {
- if (this.cause != this)//一旦通過初始化或者initCause設定了cause,就不能再次設定了
- throw new IllegalStateException("Can't overwrite cause");
- if (cause == this)//如果 cause 是此 throwable。(throwable 不能是它自己的 cause。)
- throw new IllegalArgumentException("Self-causation not permitted");
- this.cause = cause;
- return this;
- }
- public void printStackTrace() {
- printStackTrace(System.err);
- }
- //先列印thia,再列印方法調用的棧軌迹,最後列印cause
- public void printStackTrace(PrintStream s) {
- synchronized (s) {
- s.println(this);
- StackTraceElement[] trace = getOurStackTrace();
- for (int i=0; i < trace.length; i++)
- s.println("\tat " + trace[i]);
- Throwable ourCause = getCause();
- if (ourCause != null)
- ourCause.printStackTraceAsCause(s, trace);
- }
- }
- //疊代列印cause
- private void printStackTraceAsCause(PrintStream s,
- StackTraceElement[] causedTrace)
- {
- // assert Thread.holdsLock(s);
- // Compute number of frames in common between this and caused
- StackTraceElement[] trace = getOurStackTrace();
- int m = trace.length-1, n = causedTrace.length-1;
- while (m >= 0 && n >=0 && trace[m].equals(causedTrace[n])) {
- m--; n--;
- }
- int framesInCommon = trace.length - 1 - m;
- s.println("Caused by: " + this);
- for (int i=0; i <= m; i++)
- s.println("\tat " + trace[i]);
- if (framesInCommon != 0)
- s.println("\t... " + framesInCommon + " more");
- // Recurse if we have a cause
- Throwable ourCause = getCause();
- if (ourCause != null)
- ourCause.printStackTraceAsCause(s, trace);
- }
- public void printStackTrace(PrintWriter s) {
- //...同上
- }
- private void printStackTraceAsCause(PrintWriter s,
- StackTraceElement[] causedTrace)
- {
- //...同上}
- public StackTraceElement[] getStackTrace() {
- return (StackTraceElement[]) getOurStackTrace().clone();
- }
- //native方法擷取方法調用的棧軌迹 一個Throwable的stackTrace是固定的
- private synchronized StackTraceElement[] getOurStackTrace() {
- // Initialize stack trace if this is the first call to this method
- if (stackTrace == null) {
- int depth = getStackTraceDepth();
- stackTrace = new StackTraceElement[depth];
- for (int i=0; i < depth; i++)
- stackTrace[i] = getStackTraceElement(i);
- }
- return stackTrace;
- }
- native int getStackTraceDepth();
- native StackTraceElement getStackTraceElement(int index);
- }
- }