<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里有個(gè)很重要的特色是Exception ,也就是說允許程序產(chǎn)生例外狀況。而在學(xué)Java 的時(shí)候,我們也只知道Exception 的寫法,卻未必真能了解不同種類的Exception 的區(qū)別。

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

      當(dāng)我們撰寫程序的時(shí)候,我們很可能會(huì)對選擇某種形式的Exception 感到困擾,到底我應(yīng)該選擇Runtime Exception 還是Checked Exception ?

      其實(shí),在運(yùn)作上,我們可以通過Class 的Method 如何產(chǎn)生某個(gè)Exception以及某個(gè)程序如何處理這個(gè)被產(chǎn)生來的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.


      首先我們先建立一個(gè)Exception:



    1 public class CException extends Exception  

    2 {  

    3     public CException() {}  

    4     public CException(String message)  

    5     {  

    6         super(message);  

    7     }
      

    8 }
     

      然后我們撰寫一個(gè)可能產(chǎn)生 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 }
     


    在這三個(gè)method 中,我們看到了method1 和method2 的程序碼內(nèi)都會(huì)產(chǎn)生Exception,但method3 的程序碼中(大括號內(nèi)),并沒產(chǎn)生Exception,但在method3 的定義中,暗示了這個(gè)method 可能產(chǎn)生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 中,并不表示這段程序碼一定會(huì)收到CException,但它的用意在于提醒呼叫者,執(zhí)行這個(gè)method 可能產(chǎn)生的意外,而使用者也必須要能針對這個(gè)意外做出相對應(yīng)的處理方式。

      當(dāng)使用者呼叫method2() 時(shí),并不需要使用try 和catch 將程序碼包起來,因?yàn)閙ethod2 的定義中,并沒有throws 任何的Exception ,如

     

    public class runtest  

    02 {  

    03     // .  

    04     public static void main(String argv[])  

    05     {  

    06         testException te = new testException();  

    07         // 不會(huì)產(chǎn)生 Exception  

    08         te.method2("Hello");  

    09         // 會(huì)產(chǎn)生 Exception  

    10         te.method2(null);  

    11     }
      

    12     //   

    13 }
     


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

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

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

    一般而言,Checked Exception 表示這個(gè)Exception 必須要被處理,也就是說程序設(shè)計(jì)者應(yīng)該已經(jīng)知道可能會(huì)收到某個(gè)Exception(因?yàn)橐猼ry catch住) ,所以程序設(shè)計(jì)者應(yīng)該能針對這些不同的Checked Exception 做出不同的處理。

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

      看看下面的例子:

     

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

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

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

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

      然而對于Runtime Exception ,有些人建議將它c(diǎn)atch 住,然后導(dǎo)向其它地方,讓程序繼續(xù)執(zhí)行下去,這種作法并非不好,但它會(huì)讓我們在某些測試工具下認(rèn)為我們的程序碼沒有問題,因?yàn)槲覀儗untime Exception "處理"掉了,事實(shí)卻不然!譬如很多人的習(xí)慣是在程序的進(jìn)入點(diǎn)后用個(gè)大大的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 }
     

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

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















     


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲国产中文在线二区三区免| 国产精品成人四虎免费视频| 人人玩人人添人人澡免费| 深夜特黄a级毛片免费播放| 国产精品亚洲精品日韩动图| 亚洲av无码兔费综合| 精品久久久久久亚洲中文字幕| 色天使色婷婷在线影院亚洲| 国产综合激情在线亚洲第一页| 无遮挡a级毛片免费看| 一级人做人a爰免费视频| 国产精品内射视频免费| 免费精品久久天干天干| 特级无码毛片免费视频尤物| 91视频免费观看高清观看完整| 玖玖在线免费视频| 久久午夜羞羞影院免费观看| 免费观看黄色的网站| 无码中文在线二区免费| 永久免费看mv网站入口| 亚洲第一永久AV网站久久精品男人的天堂AV| 亚洲国产成人影院播放| 国产亚洲av人片在线观看| 亚洲成人午夜在线| 亚洲人成7777影视在线观看| 国产精品亚洲综合五月天| 国产综合成人亚洲区| 成人免费ā片在线观看| 久久国产乱子伦精品免费不卡| 波多野结衣免费在线| 国产成人高清精品免费鸭子 | 中国性猛交xxxxx免费看| a国产成人免费视频| 中文字幕成人免费视频| AV片在线观看免费| 亚洲美女高清一区二区三区| 亚洲精品你懂的在线观看| 亚洲另类视频在线观看| 麻豆69堂免费视频| 无码av免费一区二区三区| 无码人妻精品一二三区免费|