Java异常之checked与unchecked

转载:http://www.javashuo.com/article/p-aurydbzo-ko.htmlhtml

简介

首先,java的异常分为Error和Exception。这两类都是接口Throwable的子类。Error及Exception及其子类之间的关系,大体能够用下图简述。java

注意事项:

1。 Error仅在java的虚拟机中发生,用户无需在程序中捕捉或者抛出Error。eclipse

2。 Exception分为通常的Exception和RuntimeException两类。这里有点让人以为别扭的是RuntimeException(Unchecked)继承于Exception(Checked)的父类。测试

PS: checked与unchecked的概念理解:spa

checked: 通常是指程序不能直接控制的外界状况,是指在编译的时候就须要检查的一类exception,用户程序中必须采用try catch机制处理或者经过throws交由调用者来处理。这类异常,主要指除了Error以及RuntimeException及其子类以外的异常。code

unchecked:是指那些不须要在编译的时候就要处理的一类异常。在java体系里,全部的Error以及RuntimeException及其子类都是unchecked异常。再形象直白的理解为不须要try catch等机制处理的异常,能够认为是unchecked的异常。htm

checked与unchecked在throwable的继承关系中体现为下图:blog

复制代码
                 +-----------+
                 | 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概念很基础,可是你们不必定都明白。。。

相关文章
相关标签/搜索