Java异常之checked与unchecked

转载:https://www.cnblogs.com/shihuc/p/5201905.html

简介

首先,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概念很基础,但是大家不一定都明白。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值