<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 110, comments - 101, trackbacks - 0, articles - 7
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
    Java里有個很重要的特色是Exception ,也就是說允許程序產生例外狀況。而在學Java 的時候,我們也只知道Exception 的寫法,卻未必真能了解不同種類的Exception 的區(qū)別。

      首先,您應該知道的是Java 提供了兩種Exception 的模式,一種是執(zhí)行的時候所產生的Exception (Runtime Exception),另外一種則是受控制的Exception (Checked Exception)。所有的Checked Exception 均從java.lang.Exception 繼承而來,而Runtime Exception 則繼承java.lang.RuntimeException 或java.lang.Error (實際上java.lang.RuntimeException 的上一層也是java.lang.Exception)。

      當我們撰寫程序的時候,我們很可能會對選擇某種形式的Exception 感到困擾,到底我應該選擇Runtime Exception 還是Checked Exception ?

      其實,在運作上,我們可以通過Class 的Method 如何產生某個Exception以及某個程序如何處理這個被產生來的Exception 來了解它們之間的差異。

         uncheckException  runtimeException 
     ArithmeticException               Arithmetic error, such as divide-by-zero.

    ArrayIndexOutOfBoundsException    Array index is out-of-bounds.

    ArrayStoreException               Assignment to an array element of an incompatible type.

    ClassCastException                Invalid cast.

    IllegalArgumentException          Illegal argument used to invoke a method.

    IllegalMonitorStateException      Illegal monitor operation, such as waiting on an unlocked thread.

    IllegalStateException             Environment or application is in incorrect state.

    IllegalThreadStateException       Requested operation not compatible with current thread state.

    IndexOutOfBoundsException         Some type of index is out-of-bounds.

    NegativeArraySizeException        Array created with a negative size.

    NullPointerException              Invalid use of a null reference.

    NumberFormatException             Invalid conversion of a string to a numeric format.

    SecurityException                 Attempt to violate security.

    StringIndexOutOfBounds            Attempt to index outside the bounds of a string.

    TypeNotPresentException           Type not found. (Added by J2SE 5.)

    UnsupportedOperationException     An unsupported operation was encountered.


    下面是CheckException
    ClassNotFoundException             Class not found.

    CloneNotSupportedException         Attempt to clone an object that does not implement the Cloneable interface.

    IllegalAccessException             Access to a class is denied.

    InstantiationException             Attempt to create an object of an abstract class or interface.

    InterruptedException               One thread has been interrupted by another thread.

    NoSuchFieldException               A requested field does not exist.

    NoSuchMethodException              A requested method does not exist.


      首先我們先建立一個Exception:



    1 public class CException extends Exception  

    2 {  

    3     public CException() {}  

    4     public CException(String message)  

    5     {  

    6         super(message);  

    7     }
      

    8 }
     

      然后我們撰寫一個可能產生 CException 的 Class:

    view sourceprint
    ?01 public class testException  

    02 {  

    03     public void method1() throws CException  

    04     {     

    05         throw new CException("Test Exception");  

    06     }
      

    07     public void method2(String msg)  

    08     {  

    09         if(msg == null)  

    10         {  

    11             throw new NullPointerException("Message is null");  

    12         }
      

    13     }
      

    14     public void method3() throws CException  

    15     {  

    16         method1();  

    17     }
      

    18     // 以下省略  

    19     //   

    20 }
     


    在這三個method 中,我們看到了method1 和method2 的程序碼內都會產生Exception,但method3 的程序碼中(大括號內),并沒產生Exception,但在method3 的定義中,暗示了這個method 可能產生CException。

      呼叫method1() 的程序,必須將method1() 包含在try 與catch 中,如:

     

    public class runtest  

    02 {  

    03     // .  

    04         public static void main(String argv[])  

    05         {  

    06         testException te = new testException();  

    07         try 

    08         {  

    09             te.method1();  

    10         }
      

    11         catch(CException ce)  

    12         {  

    13             // .  

    14         }
      

    15     }
      

    16     //   

    17 }
     

    雖然包含在try 與catch 中,并不表示這段程序碼一定會收到CException,但它的用意在于提醒呼叫者,執(zhí)行這個method 可能產生的意外,而使用者也必須要能針對這個意外做出相對應的處理方式。

      當使用者呼叫method2() 時,并不需要使用try 和catch 將程序碼包起來,因為method2 的定義中,并沒有throws 任何的Exception ,如

     

    public class runtest  

    02 {  

    03     // .  

    04     public static void main(String argv[])  

    05     {  

    06         testException te = new testException();  

    07         // 不會產生 Exception  

    08         te.method2("Hello");  

    09         // 會產生 Exception  

    10         te.method2(null);  

    11     }
      

    12     //   

    13 }
     


    程序在執(zhí)行的時候,也不見得會真的產生NullPointerException ,這種Exception 叫做runtime exception 也有人稱為unchecked exception ,產生Runtime Exception 的method (在這個范例中是method2) 并不需要在宣告method 的時候定義它將會產生哪一種Exception 。

      在testException 的method3() 中,我們看到了另外一種狀況,也就是method3里呼叫了method1() ,但卻沒有將method1 包在try 和catch 之間。相反,在method3() 的定義中,它定義了CException,實際上就是如果method3 收到了CException ,它將不處理這個CException ,而將它往外丟。當然,由于method3 的定義中有throws CException ,因此呼叫method3 的程序碼也需要有try catch 才行。

      因此從程序的運作機制上看,Runtime Exception與Checked Exception 不一樣,然而從邏輯上看,Runtime Exception 與Checked Exception 在使用的目的上也不一樣。

    一般而言,Checked Exception 表示這個Exception 必須要被處理,也就是說程序設計者應該已經知道可能會收到某個Exception(因為要try catch住) ,所以程序設計者應該能針對這些不同的Checked Exception 做出不同的處理。

      而Runtime Exception 通常會暗示著程序上的錯誤,這種錯誤會導致程序設計者無法處理,而造成程序無法繼續(xù)執(zhí)行下去。

      看看下面的例子:

     

    String message[] = {"message1""message2","message3"};  

    2 System.out.println(message[3]); 

    這段程序碼在Compile 時并沒問題,但在執(zhí)行時則會出現(xiàn)ArrayIndexOutOfBoundException 的例外,在這種狀況下,我們亦無法針對這個Runtime Exception 做出有意義的動作,這就像是我們呼叫了testException 中的method2 ,卻引發(fā)了它的NullPointerException 一樣,在這種狀況下,我們必須對程序碼進行修改,從而避免這個問題。

      因此,實際上我們應該也必須要去抓取所有的Checked Exception,同時最好能在這些Checked Exception 發(fā)生的時候做出相對應的處理,好讓程序能面對不同的狀況。

      然而對于Runtime Exception ,有些人建議將它catch 住,然后導向其它地方,讓程序繼續(xù)執(zhí)行下去,這種作法并非不好,但它會讓我們在某些測試工具下認為我們的程序碼沒有問題,因為我們將Runtime Exception "處理"掉了,事實卻不然!譬如很多人的習慣是在程序的進入點后用個大大的try catch 包起來,如:

     

    public class runtest1  

    02 {  

    03     public static void main(String argv[])  

    04     {  

    05         try 

    06         {  

    07             //  

    08         }
      

    09         catch(Exception e)  

    10         {  

    11         }
      

    12     }
      

    13 }
     

    在這種情況下,我們很可能會不知道發(fā)生了什么Exception 或是從哪一行發(fā)出的,因此在面對不同的Checked Exception時,我們可已分別去try catch它。而在測試階段時,如果碰到Runtime Exception ,我們可以讓它就這樣發(fā)生,接著再去修改我們的程序碼,讓它避免Runtime Exception,否則,我們就應該仔細追究每一個Exception ,直到我們可以確定它不會有Runtime Exception 為止!

      對于Checked Exception 與Runtime Exception ,我想應該有不少人會有不同的觀點,無論如何,程序先要能執(zhí)行,這些Exception 才有機會產生。因此,我們可以把這些Exception 當成是Bug ,也可以當成是不同的狀況(Checked Exception),或當成是幫助我們除錯的工具(Runtime Exception),但前提是我們需要處理這些Exception ,如果不處理,那么問題或狀況就會永遠留在那里















     


    只有注冊用戶登錄后才能發(fā)表評論。


    網站導航:
     
    主站蜘蛛池模板: 国产一区二区三区在线免费观看| 日本免费高清一本视频| 亚洲精品视频在线免费| 美女被免费喷白浆视频| 日本视频免费观看| 亚洲精品私拍国产福利在线| 精品国产免费观看| 精品亚洲永久免费精品| 亚洲老熟女五十路老熟女bbw| 亚洲日本中文字幕天堂网| 最近免费中文字幕mv在线电影| 老子影院午夜伦不卡亚洲| 免费日韩在线视频| 亚洲永久永久永久永久永久精品| 亚洲精品中文字幕乱码三区| 又硬又粗又长又爽免费看 | 天堂亚洲免费视频| 久久狠狠高潮亚洲精品| 免费一看一级毛片全播放| 最近最好最新2019中文字幕免费 | 国产永久免费高清在线| 亚洲熟妇AV一区二区三区宅男| 国产亚洲免费的视频看| 精品久久洲久久久久护士免费| 久久精品视频免费播放| 国产精品亚洲专区无码不卡| 亚洲福利视频网址| 亚洲线精品一区二区三区影音先锋| 天天看免费高清影视| 99在线观看精品免费99| 国产免费一区二区三区免费视频 | 91精品免费不卡在线观看| 无码精品人妻一区二区三区免费| 亚洲中字慕日产2020| 亚洲国产成人片在线观看| 亚洲精品GV天堂无码男同| 国产gav成人免费播放视频| 国产免费久久精品丫丫| 美国毛片亚洲社区在线观看 | 日韩国产精品亚洲а∨天堂免| 久久亚洲私人国产精品|