check and Unchecked Exception

  1. Checked异常必须被显式地捕获或者传递,如Basic try-catch-finally Exception Handling一文中所说。而unchecked异常则可以不必捕获或抛出。
  2. Checked异常继承java.lang.Exception类。Unchecked异常继承自java.lang.RuntimeException类。
  3.  除了Error与RuntimeException,其他剩下的异常都是你需要关心的,而这些异常类统称为Checked Exception,至于Error与RuntimeException则被统称为Unchecked Exception。

首先,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

package check_unchecked_exceptions;

public class GenericException extends Exception{

    /**
     * 
     */
    private static final long serialVersionUID = 2778045265121433720L;
    
    public GenericException(){
        
    }
    
    public GenericException(String msg){
        super(msg);
    }
}
下面定义一个测试类VerifyException。
package check_unchecked_exceptions;

public class VerifyException {

    public void first() throws GenericException {
        throw new GenericException("checked exception");
    }
    
    public void second(String msg){
        if(msg == null){
            throw new NullPointerException("unchecked exception");
        }
    }
    
    public void third() throws GenericException{
        first();
    }
    
    public static void main(String[] args) {
        VerifyException ve = new VerifyException();
        
        try {
            ve.first();
        } catch (GenericException e) {
            e.printStackTrace();
        }

        ve.second(null);
    }

}
运行后,在eclipse的控制台上得到下面的信息:
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的。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值