Java異常之checked與unchecked


首先,java的異常分為Error和Exception。這兩類都是接口Throwable的子類。Error及Exception及其子類之間的關系,大致可以用下圖簡述。

注意事項:

1。 Error僅在java的虛擬機中發生,用戶無需在程序中捕捉或者拋出Error。

2。 Exception分為一般的Exception和RuntimeException兩類。這里有點讓人覺得別扭的是RuntimeException(Unchecked)繼承於Exception(Checked)的父類。

PS: checked與unchecked的概念理解:

checked: 一般是指程序不能直接控制的外界情況,是指在編譯的時候就需要檢查的一類exception,用戶程序中必須采用try catch機制處理或者通過throws交由調用者來處理。這類異常,主要指除了Error以及RuntimeException及其子類之外的異常。

unchecked:是指那些不需要在編譯的時候就要處理的一類異常。在java體系里,所有的Error以及RuntimeException及其子類都是unchecked異常。再形象直白的理解為不需要try catch等機制處理的異常,可以認為是unchecked的異常。

checked與unchecked在throwable的繼承關系中體現為下圖:

                 +-----------+
                 | Throwable |
                 +-----------+
                  /         \
                 /           \
          +-------+          +-----------+
          | Error |          | Exception |
          +-------+          +-----------+
           /  |  \           / | \        \
          \________/       \______/         \
                                        +------------------+
          unchecked        checked      | RuntimeException |
                                        +------------------+
                                         /   |    |      \
                                        \_________________/
                        
                                            unchecked

 

下面列舉例子說明上面的注意事項2中提到的比較別扭的地方:

首先定義一個基本的異常類GenericException,繼承於Exception。

 1 package check_unchecked_exceptions;
 2 
 3 public class GenericException extends Exception{
 4 
 5     /**
 6      * 
 7      */
 8     private static final long serialVersionUID = 2778045265121433720L;
 9     
10     public GenericException(){
11         
12     }
13     
14     public GenericException(String msg){
15         super(msg);
16     }
17 }

下面定義一個測試類VerifyException。

 1 package check_unchecked_exceptions;
 2 
 3 public class VerifyException {
 4 
 5     public void first() throws GenericException {
 6         throw new GenericException("checked exception");
 7     }
 8     
 9     public void second(String msg){
10         if(msg == null){
11             throw new NullPointerException("unchecked exception");
12         }
13     }
14     
15     public void third() throws GenericException{
16         first();
17     }
18     
19     public static void main(String[] args) {
20         VerifyException ve = new VerifyException();
21         
22         try {
23             ve.first();
24         } catch (GenericException e) {
25             e.printStackTrace();
26         }
27 
28         ve.second(null);
29     }
30 
31 }

運行后,在eclipse的控制台上得到下面的信息:

1 check_unchecked_exceptions.GenericException: checked exception
2     at check_unchecked_exceptions.VerifyException.first(VerifyException.java:6)
3     at check_unchecked_exceptions.VerifyException.main(VerifyException.java:23)
4 Exception in thread "main" java.lang.NullPointerException: unchecked exception
5     at check_unchecked_exceptions.VerifyException.second(VerifyException.java:11)
6     at check_unchecked_exceptions.VerifyException.main(VerifyException.java:29)

上面的例子,結合checked以及unchecked的概念,可以看出Exception這個父類是checked類型,但是其子類RuntimeException (子類NullPointerException)卻是unchecked的。

 

本博文描述的checked與unchecked概念很基礎,但是大家不一定都明白。。。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM