b47617

          攀登!進步!
          隨筆 - 20, 文章 - 1, 評論 - 0, 引用 - 0
          數(shù)據(jù)加載中……

          Java中的異常處理機制的簡單原理和應用~~

                JAVA程序違反了JAVA的語義規(guī)則時,JAVA虛擬機就會將發(fā)生的錯誤表示為一個異常。違反語義規(guī)則包括2種情況。一種是JAVA類庫內(nèi)置的語義檢查。例如數(shù)組下標越界,會引發(fā)IndexOutOfBoundsException;訪問null的對象時會引發(fā) NullPointerException。另一種情況就是JAVA允許程序員擴展這種語義檢查,程序員可以創(chuàng)建自己的異常,并自由選擇在何時用 throw關鍵字引發(fā)異常。所有的異常都是java.lang.Thowable的子類。
          eg:第一種
          class Test{
                   public int devide(int x ,int y) throws Exception{  //
                            int result = x/y;
                            return result;
                   }         
          }
          class TestException{
                   public static void main(String[] args){
                            try{
                                     new Test().devide(3,0);
                            }catch(Exception e){
                                     System.out.println(e.getMessage());//雖然Exception除了Constructor之外沒有其它的函數(shù),
          //但它繼承了Throwable類.
                              }
                            System.out.println("Program running here");
                   }
          }
          當編寫Test class不知道它是否拋出異常時,可以在devide methods方法后添加 throws Exception 就可以..而在TestException中必須處理拋出的異常.
          eg:第二種
          如果自己定義異常類,必須繼承Exception,也就是它的子類.
          class Test{
                   public int devide(int x ,int y) throws Exception{  //
                            if(y < 0) 
                                     throw DevideMyMinusException("devide is" + y);
                            int result = x/y;
                            return result;
                   }         
          }
          class DevideMyMinusException extends Exception{
                      public DevideMyMinusException (String msg){
                               super(msg);  //調(diào)用父類(Exception)的Construtor,主函數(shù)getMessage()輸出自定義的異常:"devide is..."
                      }
          }
          eg.第三種
          可以拋出多個異常類.
          class Test{
                   public int devide(int x ,int y) throws ArithmeticException,DevideMyMinusException{  //
                            if(y < 0) 
                                     throw new DevideMyMinusException("devide is" + y);
                            int result = x/y;
                            return result;
                   }         
          }
          class DevideMyMinusException extends Exception{
                      public DevideMyMinusException (String msg){
                               super(msg); 
                      }
          }

          class TestException{
                   public static void main(String[] args){
                            try{
                                     new Test().devide(3,-1);
                            }catch(ArithmeticException e){
                                     System.out.println("program running into ArithmeticException ");
                                     e.printStackTrace();
                            }catch(DevideMyMinusException e){
                                     System.out.println("program running into DevideMyMinusException");
                                     e.printStackTrace();
                            }catch(Exception e){
                                     System.out.println(e.getMessage)
                            )finally{
                                     System.out.println("finally running");//不管程序發(fā)生異常沒有,finally代碼塊都要執(zhí)行.
                            }


                            System.out.println("Program running here");
                   }
          }
          1.程序根據(jù)異常類型的匹配.自動進入相應的catch語句.Exception應放在其它異常語句后,因為他們都繼承Exception ,其它異常要放在后面,就沒有什么意義了.
          2.try {}里有一個return語句,那么緊跟在這個try后的finally {}里的code會不會被執(zhí)行,什么時候被執(zhí)行,在return前還是后? 會執(zhí)行,而且在return 前執(zhí)行.
          3.什么時候finally里的代碼不會執(zhí)行呢? 當出現(xiàn)System.exit(0);時,它不會執(zhí)行,程序會退出.

          posted on 2006-03-04 02:30 原語 閱讀(1966) 評論(0)  編輯  收藏


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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 桐柏县| 汉阴县| 申扎县| 和静县| 云和县| 庆安县| 壤塘县| 汤原县| 同德县| 绥化市| 鹤峰县| 岐山县| 朝阳区| 聊城市| 老河口市| 加查县| 靖远县| 搜索| 尚义县| 云林县| 沁源县| 肃北| 渭源县| 岐山县| 三穗县| 祥云县| 吉安市| 襄垣县| 北海市| 淳安县| 吉木乃县| 鸡西市| 永登县| 全椒县| 剑川县| 大同市| 明溪县| 剑阁县| 桃园县| 喀喇沁旗| 南平市|