Java知多少(48)try語句的嵌套

來源:互聯網
上載者:User

標籤:

Try語句可以被嵌套。也就是說,一個try語句可以在另一個try塊內部。每次進入try語句,異常的前後關係都會被推入堆棧。如果一個內部的try語句不含特殊異常的catch處理常式,堆棧將彈出,下一個try語句的catch處理常式將檢查是否與之匹配。這個過程將繼續直到一個catch語句匹配成功,或者是直到所有的嵌套try語句被檢查耗盡。如果沒有catch語句匹配,Java的運行時系統將處理這個異常。下面是運用嵌套try語句的一個例子:

 1 // An example of nested try statements. 2 class NestTry { 3     public static void main(String args[]) { 4         try { 5             int a = args.length; 6             /* If no command-line args are present,the following statement will generate a divide-by-zero exception. */ 7             int b = 42 / a; 8             System.out.println("a = " + a); 9             try { // nested try block10                 /* If one command-line arg is used,then a divide-by-zero exception will be generated by the following code. */11                 if(a==1) a = a/(a-a); // division by zero12                 /* If two command-line args are used,then generate an out-of-bounds exception. */13                 if(a==2) {14                     int c[] = { 1 };15                     c[42] = 99; // generate an out-of-bounds exception16                 }17             } catch(ArrayIndexOutOfBoundsException e) {18                 System.out.println("Array index out-of-bounds: " + e);19             }20         } catch(ArithmeticException e) {21             System.out.println("Divide by 0: " + e);22         }23     }24 }

如你所見,該程式在一個try塊中嵌套了另一個try塊。程式工作如下:當你在沒有命令列參數的情況下執行該程式,外面的try塊將產生一個被零除的異常。程式在有一個命令列參數條件下執行,由嵌套的try塊產生一個被零除的錯誤。因為內部的塊不匹配這個異常,它將把異常傳給外部的try塊,在那裡異常被處理。如果你在具有兩個命令列參數的條件下執行該程式,由內部try塊產生一個數組邊界異常。下面的結果闡述了每一種情況:

C:\>java NestTryDivide by 0: java.lang.ArithmeticException: / by zeroC:\>java NestTry Onea = 1Divide by 0: java.lang.ArithmeticException: / by zeroC:\>java NestTry One Twoa = 2Array index out-of-bounds: java.lang.ArrayIndexOutOfBoundsException

當有方法調用時,try語句的嵌套可以很隱形發生。例如,你可以把對方法的調用放在一個try塊中。在該方法內部,有另一個try語句。這種情況下,方法內部的try仍然是嵌套在外部調用該方法的try塊中的。下面是前面例子的修改,嵌套的try塊移到了方法nesttry( )的內部:

 1 /* Try statements can be implicitly nested via calls to methods. */ 2 class MethNestTry { 3     static void nesttry(int a) { 4         try { // nested try block 5             /* If one command-line arg is used,then a divide-by-zero exception will be generated by the following code. */ 6             if(a==1) a = a/(a-a); // division by zero 7             /* If two command-line args are used,then generate an out-of-bounds exception. */ 8             if(a==2) { 9                 int c[] = { 1 };10                 c[42] = 99; // generate an out-of-bounds exception11             }12         } catch(ArrayIndexOutOfBoundsException e) {13             System.out.println("Array index out-of-bounds: " + e);14         }15     }16 17     public static void main(String args[]) {18         try {19             int a = args.length;20            /* If no command-line args are present,the following statement will generate a divide-by-zero exception. */21            int b = 42 / a;22            System.out.println("a = " + a);23            nesttry(a);24         } catch(ArithmeticException e) {25             System.out.println("Divide by 0: " + e);26         }27     }28 }

該程式的輸出與前面的例子相同。

系列文章:

Java知多少(上)

Java知多少(39)interface介面

Java知多少(40)介面和抽象類別的區別

Java知多少(41)泛型詳解

Java知多少(42)泛型萬用字元和型別參數的範圍

Java知多少(43)異常處理基礎

Java知多少(44)異常類型

Java知多少(45)未被捕獲的異常

Java知多少(46)try和catch的使用

Java知多少(47)多重catch語句的使用

Java知多少(48)try語句的嵌套

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.