JAVAプログラミング——Throwableのよくある異常とソースコードの詳しい解


<span style="font-family:Microsoft YaHei;font-size:14px;"><span lang="EN-US">Throwable</span> <span lang="EN-US">java.lang</span>              。      , <span lang="EN-US">Error </span> <span lang="EN-US">Exception</span>,            。</span><span style="font-family:Microsoft YaHei;font-size:14px;">
</span>
<span style="font-family:Microsoft YaHei;font-size:12px;"><span lang="EN-US">Error</span>               ,             ,           。<span lang="EN-US"> 
Exception</span> <span lang="EN-US">Throwable</span>       。                                                                                                                                                                           <span style="white-space:pre">	</span></span><span style="font-family:Microsoft YaHei;font-size:12px;"><span lang="EN-US">Exception</span>      ,            <span lang="EN-US">Java</span>                 ,        。<span>							</span><span lang="EN-US">Exception </span>              <span lang="EN-US">Java</span>             (           <span lang="EN-US">Exception</span> ),           。   <span style="white-space:pre">	</span>          、            。</span><span lang="EN-US" style="font-family: Arial, Helvetica, sans-serif;"><span style="font-family:Microsoft YaHei;font-size:12px;"> </span></span>
<span lang="EN-US"><span style="font-family:Microsoft YaHei;"><span style="font-size:12px;">
</span></span><span style="font-family:Microsoft YaHei;font-size:12px;">    </span></span><span style="font-family:Microsoft YaHei;font-size:12px;">                     :<span lang="EN-US"> 
    ArithmeticException——</span>     0     ;<span lang="EN-US"> 
    ArrayStoreException——</span>               ;<span lang="EN-US"> 
    ClassCastException—</span>            ,                  ,         ,      ;<span lang="EN-US"> 
    IllegalMonitorStateException——</span>            ;<span lang="EN-US"> 
    NegativeArraySizeException—</span>        ,     ;<span lang="EN-US"> 
    NullPointerException—</span>                                     ;<span lang="EN-US"> 
    OutofMemoryException——</span> <span lang="EN-US">new</span>       ,                  ;<span lang="EN-US"> 
    SecurityException——</span>            ,            ;<span lang="EN-US"> 
    IndexOutOfBoundsExcention——</span>                    ;<span lang="EN-US"> 
    IOException——</span>       、     <span lang="EN-US">I/O</span>           ;<span lang="EN-US"> 
    ClassNotFoundException——</span>                ;<span lang="EN-US"> 
    CloneNotSupportedException——</span>           <span lang="EN-US">Object</span>  <span lang="EN-US">clone</span>  ,          <span lang="EN-US">Cloneable</span>  ,      ;<span lang="EN-US"> 
    InterruptedException—</span>             ,          ,       ,       ,       ;<span lang="EN-US"> 
    NoSuchMethodException</span>          ,    ;<span lang="EN-US"> 
    Illega1AccessExcePtion—</span>        <span lang="EN-US">public</span>  ;<span lang="EN-US"> 
    StringIndexOutOfBoundsException——</span>         ,    ;<span lang="EN-US"> 
    ArrayIdexOutOfBoundsException—</span>           ,    ;<span lang="EN-US"> 
    NumberFormatException——</span>   <span lang="EN-US">UTF</span>            ;<span lang="EN-US"> 
    IllegalThreadException—</span>                 ,    ;<span lang="EN-US"> 
    FileNotFoundException——</span>           ;<span lang="EN-US"> 
    EOFException——</span>                 。</span><span style="font-family:Microsoft YaHei;font-size:14px;">
</span>
<span style="font-family:Microsoft YaHei;font-size:14px;">    :</span>
<span style="font-family:Microsoft YaHei;font-size:14px;">	package java.lang;  
	import java.io.*;  
	/** 
	*  
	* Throwable   Error Exceptiong    
	*           : 
	* Throwable() 
	* Throwable(String message) 
	* Throwable(Throwable cause) 
	* Throwable(String message, Throwable cause) 
	*  
	*/  
	public class Throwable implements Serializable {  
	      private static final long serialVersionUID = -3042686055658047285L;  
	  
	      /** 
	       * Native code saves some indication of the stack backtrace in this slot. 
	       */  
	      private transient Object backtrace;   
	  
	      /** 
	       *          
	       */  
	      private String detailMessage;  
	  
	      /** 
	       *          Throwable   
	        *    null          Throwable    
	        *           ,                  
	       */  
	      private Throwable cause = this;  
	  
	      /** 
	       *           
	       */  
	      private StackTraceElement[] stackTrace;  
	  
	      /** 
	       *     ,                 initCause      
	        * fillInStackTrace                 
	       */  
	      public Throwable() {  
	          fillInStackTrace();  
	      }  
	  
	      /** 
	       *      
	       */  
	      public Throwable(String message) {  
	         //          
	          fillInStackTrace();  
	         //           
	          detailMessage = message;  
	      }  
	  
	      /** 
	       *     ,cause       
	       */  
	      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();  
	      }  
	  
	      /** 
	       *        
	       */  
	      public Throwable getCause() {  
	          return (cause==this ? null : cause);  
	      }  
	  
	      /** 
	       *        ,                     
	       */  
	      public synchronized Throwable initCause(Throwable cause) {  
	         //                 
	          if (this.cause != this)  
	              throw new IllegalStateException("Can't overwrite cause");  
	          
	        //                    
	          if (cause == this)  
	              throw new IllegalArgumentException("Self-causation not permitted");  
	          
	         //        
	          this.cause = cause;  
	         //            
	          return this;  
	      }  
	  
	      /** 
	       *         
	       */  
	      public String toString() {       
	          String s = getClass().getName();          
	          String message = getLocalizedMessage();        
	          return (message != null) ? (s + ": " + message) : s;  
	      }  
	  
	      /** 
	       *         
	       */  
	      public void printStackTrace() {   
	          printStackTrace(System.err);  
	      }  
	  
	      /** 
	       *         
	       */  
	      public void printStackTrace(PrintStream s) {  
	          synchronized (s) {  
	            //       toString    
	              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);  
	          }  
	      }  
	  
	      /** 
	       *           
	       * @param s      
	        * @param causedTrace                 
	       */  
	      private void printStackTraceAsCause(PrintStream s,  
	                                          StackTraceElement[] causedTrace)  
	      {  
	         //           
	          StackTraceElement[] trace = getOurStackTrace();  
	         //m                  ,   
	         //n                          
	          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");  
	  
	         //          ,          
	          Throwable ourCause = getCause();  
	          if (ourCause != null)  
	              ourCause.printStackTraceAsCause(s, trace);  
	      }  
	  
	      /** 
	       *         
	       */  
	      public void printStackTrace(PrintWriter 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);  
	          }  
	      }  
	  
	      /** 
	       *           
	        */  
	      private void printStackTraceAsCause(PrintWriter 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 synchronized native Throwable fillInStackTrace();  
	  
	      /** 
	       *              
	       */  
	      public StackTraceElement[] getStackTrace() {  
	          return (StackTraceElement[]) getOurStackTrace().clone();  
	      }  
	  
	      
	      /** 
	       *           
	        */  
      private synchronized StackTraceElement[] getOurStackTrace() {  
	         //                      
	          if (stackTrace == null) {  
	            //          
	              int depth = getStackTraceDepth();  
	            //          ,      
	              stackTrace = new StackTraceElement[depth];  
	              
	            for (int i=0; i < depth; i++)  
	                stackTrace[i] = getStackTraceElement(i);//             
	          }  
	          
	          return stackTrace;  
	      }  
	  
	      /** 
	       *        
	       */  
	      public void setStackTrace(StackTraceElement[] stackTrace) {  
	         //        
	          StackTraceElement[] defensiveCopy =  
	              (StackTraceElement[]) stackTrace.clone();  
	          
	         //                 
	          for (int i = 0; i < defensiveCopy.length; i++)  
	              if (defensiveCopy[i] == null)  
	                  throw new NullPointerException("stackTrace[" + i + "]");  
	  
	         //             
	          this.stackTrace = defensiveCopy;  
	      }  
	  
	      /** 
	       *        ,0       
	       */  
	      private native int getStackTraceDepth();  
	  
	      /** 
	       *             
	       */  
	      private native StackTraceElement getStackTraceElement(int index);  
	  
	      
	      private synchronized void writeObject(java.io.ObjectOutputStream s)  
	          throws IOException  
	      {  
	          getOurStackTrace();  
	          s.defaultWriteObject();  
	      }  
	}  
</span>