凡是會拋出例外 (exception) 的方法 (method) , Java 利用 try-catch 陳述 (statement) 讓程式設計師自行處理例外。 try-catch 為關鍵字 (keyword) 之一,專門用來例外處理 (exception handling) 的。
基本形式就是把可能會產生例外的程式碼放在 try 之後的程式區塊 (block) , catch 則放例外發生時的處置,如下例
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
 | class Try1Demo {    public static void main(String[] args) {        try {            System.out.println(1 / 0);        }        catch (Exception ex) {            System.out.println("something wrong");        }    }}/* 《程式語言教學誌》的範例程式    檔名:Try1Demo.java    功能:示範物件導向的基本觀念     作者:張凱慶    時間:西元 2010 年 10 月 */ | 
編譯後執行,結果如下

由於第 4 行,這裡原本會產生除以 0 的致命錯誤,但因為有 try-catch 的例外處理,所以發生例外是執行 catch 部份印出 "something wrong" 的字串 (string) 。
除以 0 的致命錯誤屬於 ArimethicException 類別,而 ArimethicException 繼承自 RuntimeException , RuntimeException 又繼承自 Exception ,事實上,所有例外均繼承自 Exception ,因此使用 Exception 可抓住所有例外。
但有時需要個別處理某些例外,例如我們可將例外直接宣告成 ArimethicException ,便可專門處理這個例外
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
 | class Try2Demo {    public static void main(String[] args) {        try {            System.out.println(1 / 0);        }        catch (ArithmeticException ex) {            System.out.println("算術錯誤的例外");        }    }}/* 《程式語言教學誌》的範例程式    檔名:Try2Demo.java    功能:示範物件導向的基本觀念     作者:張凱慶    時間:西元 2010 年 10 月 */ | 
編譯後執行,結果如下

有時許多不同的例外需要處理,我們可以依需要增加 catch 的數量,例如
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
 | class Try3Demo {    public static void main(String[] args) {        try {            System.out.println(1 / 0);        }        catch (ArithmeticException ex) {            System.out.println("算術錯誤的例外");        }        catch (Exception ex) {            System.out.println("something wrong");        }    }}/* 《程式語言教學誌》的範例程式    檔名:Try3Demo.java    功能:示範物件導向的基本觀念     作者:張凱慶    時間:西元 2010 年 10 月 */ | 
編譯後執行,結果如下

多個 catch 從上到下,上面的會優先處理,一旦抓到例外,就會執行該 catch 區塊的部份,其餘會跳過。
try-catch 複合陳述中,有些 catch 會被執行,有 catch 則不會,如果例外處理時有無論如何一定要執行的部份,這時可加進另一個關鍵字 finally ,將一定要執行的部份放到這裡,如
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
 | class Try4Demo {    public static void main(String[] args) {        try {            System.out.println(1 / 0);        }        catch (ArithmeticException ex) {            System.out.println("算術錯誤的例外");        }        catch (Exception ex) {            System.out.println("something wrong");        }        finally {            System.out.println("無論如何都會印出的訊息....");        }    }}/* 《程式語言教學誌》的範例程式    檔名:Try4Demo.java    功能:示範物件導向的基本觀念     作者:張凱慶    時間:西元 2010 年 10 月 */ | 
編譯後執行,結果如下

| 中英文術語對照 | |
|---|---|
| 例外 | exception | 
| 方法 | method | 
| 陳述 | statement | 
| 關鍵字 | keyword | 
| 例外處理 | exception handling | 
| 區塊 | block | 
| 字串 | string | 
參考資料
http://download.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html
http://download.oracle.com/javase/tutorial/essential/exceptions/handling.html
http://download.oracle.com/javase/tutorial/essential/exceptions/try.html
http://download.oracle.com/javase/tutorial/essential/exceptions/catch.html
http://download.oracle.com/javase/tutorial/essential/exceptions/finally.html
http://download.oracle.com/javase/tutorial/essential/exceptions/putItTogether.htm
http://download.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html
http://download.oracle.com/javase/tutorial/essential/exceptions/handling.html
http://download.oracle.com/javase/tutorial/essential/exceptions/try.html
http://download.oracle.com/javase/tutorial/essential/exceptions/catch.html
http://download.oracle.com/javase/tutorial/essential/exceptions/finally.html
http://download.oracle.com/javase/tutorial/essential/exceptions/putItTogether.htm
沒有留言:
張貼留言