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的异常。

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

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

(1) 首先定义一个基本的异常类GenericException,继承于Exception。

package checked_unchecked;

/**
 * @desc 定义基本异常类继承Exception
 */
public class GenericException extends Exception {

    private static final long serialVersionUID = 2778045265121433720L;

    public GenericException() {

    }

    public GenericException(String message) {
        super(message);
    }
}

(2) 接下来定义一个测试类

package checked_unchecked;

public class VerifyExceptionTest {

    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) {
        VerifyExceptionTest vet = new VerifyExceptionTest();
        try {
            vet.first();
        } catch (GenericException ge) {
            ge.printStackTrace();
        }

        vet.second(null);
    }


}

(3) 运行后,在IDE的控制台上得到下面的信息:

checked_unchecked.GenericException: checked Exception
	at checked_unchecked.VerifyExceptionTest.first(VerifyExceptionTest.java:6)
	at checked_unchecked.VerifyExceptionTest.main(VerifyExceptionTest.java:22)
Exception in thread "main" java.lang.NullPointerException: unchecked Exception
	at checked_unchecked.VerifyExceptionTest.second(VerifyExceptionTest.java:11)
	at checked_unchecked.VerifyExceptionTest.main(VerifyExceptionTest.java:27)

上面的例子,结合checked以及unchecked的概念,可以看出Exception这个父类是checked类型,但是其子类RuntimeException (子类NullPointerException)却是unchecked的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值