java exception分类

java exception分类

1、Throwable 结构图

在这里插入图片描述

2、可检查与非检查异常:

1、可检查异常(checked exceptions)是程序运行过程中很容易出现的的异常情况,编译器要求必须捕获处理。这种错误在一定程度上是可以预见的,如IOException,FileNotFoundException等。

2、不可检查异常(unchecked exceptions)是编译器不强制要求处理的异常,主要包括运行时异常(RuntimeException与其子类)和错误(Error)。

3、运行与非运行异常:

1、运行时异常:都是RuntimeException类及其子类异常,如NullPointerException(空指针异常)IndexOutOfBoundsException(下标越界异常)等,

2、非运行时异常(编译异常):是RuntimeException以外的异常,类型上都属于Exception类及其子类。

4、自定义异常

public class MyException extends RuntimeException{

    private String msg;

    public MyException(){

    }

    public MyException(String msg){
        super();
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

/**
* 测试
*/
public static void main(String[] args){
        try {
            testException();
        }catch (MyException e){
            System.out.println(e.getMsg());
        }
    }

    public static void testException(){
        try {
            int a = 10/0;
        }catch (Exception e){
            throw new MyException("exception test");
        }
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值