天天看点

Throwable学习

一.java异常类结构

Throwable学习

常用的CheckedException

Throwable学习

常用的RuntimeException

Throwable学习

二.Throwable类源码

  1.  StackTraceElement。一个final类,代表栈轨迹中的元素,一个异常可能有多个元素。 Java代码  
    Throwable学习
    1. public final class StackTraceElement implements java.io.Serializable {  
    2.     // Normally initialized by VM (public constructor added in 1.5)  
    3.     private String declaringClass;//com.jyz.study.jdk.exception.ExceptionTest  
    4.     private String methodName;//equals  
    5.     private String fileName;//ExceptionTest.java  
    6.     private int    lineNumber;//17  
    7.     //可以看到 native方法lineNumber=-2  
    8.     public boolean isNativeMethod() {  
    9.         return lineNumber == -2;  
    10.     }  
    11.     //com.jyz.study.jdk.exception.ExceptionTest.equals(ExceptionTest.java:17)  
    12.     public String toString() {  
    13.         return getClassName() + "." + methodName +  
    14.             (isNativeMethod() ? "(Native Method)" :  
    15.              (fileName != null && lineNumber >= 0 ?  
    16.               "(" + fileName + ":" + lineNumber + ")" :  
    17.               (fileName != null ?  "("+fileName+")" : "(Unknown Source)")));  
    18.     }  
    19. }  
  2. Throwable。java里所有错误和异常的基类。 Java代码  
    Throwable学习
    1. public class Throwable implements Serializable {  
    2.    //异常详细信息  
    3.     private String detailMessage;  
    4.     //初始化异常原因case为本身  
    5.     private Throwable cause = this;  
    6.     //栈轨迹中的元素  
    7.     private StackTraceElement[] stackTrace;  
    8.     //一般也就四个构造函数  
    9.     public Throwable() {  
    10.         fillInStackTrace();  
    11.     }  
    12.     public Throwable(String message) {  
    13.         fillInStackTrace();  
    14.         detailMessage = message;  
    15.     }  
    16.     public Throwable(String message, Throwable cause) {  
    17.         fillInStackTrace();  
    18.         detailMessage = message;  
    19.         this.cause = cause;  
    20.     }  
    21.     public Throwable(Throwable cause) {  
    22.         fillInStackTrace();  
    23.         detailMessage = (cause==null ? null : cause.toString());  
    24.         this.cause = cause;  
    25.     }  
    26.     //自定义异常时  可以重写此方法  
    27.     public String getMessage() {  
    28.         return detailMessage;  
    29.     }  
    30.     //也可以重写此方法,比喻绑定国际化资源  
    31.     public String getLocalizedMessage() {  
    32.         return getMessage();  
    33.     }  
    34.     //if cause == null return null  
    35.     public Throwable getCause() {  
    36.         return (cause==this ? null : cause);  
    37.     }  
    38.     //构造异常链  
    39.    public synchronized Throwable initCause(Throwable cause) {  
    40.         if (this.cause != this)//一旦通过初始化或者initCause设置了cause,就不能再次设置了  
    41.             throw new IllegalStateException("Can't overwrite cause");  
    42.         if (cause == this)//如果 cause 是此 throwable。(throwable 不能是它自己的 cause。)  
    43.             throw new IllegalArgumentException("Self-causation not permitted");  
    44.         this.cause = cause;  
    45.         return this;  
    46.     }  
    47.     public void printStackTrace() {  
    48.         printStackTrace(System.err);  
    49.     }  
    50.     //先打印thia,再打印方法调用的栈轨迹,最后打印cause  
    51.     public void printStackTrace(PrintStream s) {  
    52.         synchronized (s) {  
    53.             s.println(this);  
    54.             StackTraceElement[] trace = getOurStackTrace();  
    55.             for (int i=0; i < trace.length; i++)  
    56.                 s.println("\tat " + trace[i]);  
    57.             Throwable ourCause = getCause();  
    58.             if (ourCause != null)  
    59.                 ourCause.printStackTraceAsCause(s, trace);  
    60.         }  
    61.     }  
    62.     //迭代打印cause  
    63.     private void printStackTraceAsCause(PrintStream s,  
    64.                                         StackTraceElement[] causedTrace)  
    65.     {  
    66.         // assert Thread.holdsLock(s);  
    67.         // Compute number of frames in common between this and caused  
    68.         StackTraceElement[] trace = getOurStackTrace();  
    69.         int m = trace.length-1, n = causedTrace.length-1;  
    70.         while (m >= 0 && n >=0 && trace[m].equals(causedTrace[n])) {  
    71.             m--; n--;  
    72.         }  
    73.         int framesInCommon = trace.length - 1 - m;  
    74.         s.println("Caused by: " + this);  
    75.         for (int i=0; i <= m; i++)  
    76.             s.println("\tat " + trace[i]);  
    77.         if (framesInCommon != 0)  
    78.             s.println("\t... " + framesInCommon + " more");  
    79.         // Recurse if we have a cause  
    80.         Throwable ourCause = getCause();  
    81.         if (ourCause != null)  
    82.             ourCause.printStackTraceAsCause(s, trace);  
    83.     }  
    84.     public void printStackTrace(PrintWriter s) {  
    85.             //...同上  
    86.     }  
    87.     private void printStackTraceAsCause(PrintWriter s,  
    88.                                         StackTraceElement[] causedTrace)  
    89.     {  
    90.         //...同上}  
    91.     public StackTraceElement[] getStackTrace() {  
    92.         return (StackTraceElement[]) getOurStackTrace().clone();  
    93.     }  
    94.      //native方法获取方法调用的栈轨迹  一个Throwable的stackTrace是固定的  
    95.     private synchronized StackTraceElement[] getOurStackTrace() {  
    96.         // Initialize stack trace if this is the first call to this method  
    97.         if (stackTrace == null) {  
    98.             int depth = getStackTraceDepth();  
    99.             stackTrace = new StackTraceElement[depth];  
    100.             for (int i=0; i < depth; i++)  
    101.                 stackTrace[i] = getStackTraceElement(i);  
    102.         }  
    103.         return stackTrace;  
    104.     }  
    105.     native int getStackTraceDepth();  
    106.     native StackTraceElement getStackTraceElement(int index);  
    107.     }