异常处理---Throwable

Throwable是类,Exception和Error都继承了该类
所以在捕捉的时候,也可以使用Throwable进行捕捉
如图: 异常分Error和Exception
Exception里又分运行时异常和可查异常。
在这里插入图片描述

public class TestException {
    public static void main(String[] args) {
        File f=new File("d:/l.txt");

        try {
            System.out.println("试图打开文件");
            new FileInputStream(f);
            //使用Throwable进行异常捕获

        } catch (Throwable a) {
            a.printStackTrace();
            System.out.println("打开文件失败");
        }
    }
}

在这里插入图片描述
在方法声明上,可以抛出指定的异常,比如FileNotFoundException
那么能否抛出Throwable这个类?

这个方法的调用者又该如何处理?

public class Demo01 {
    public static void main(String[] args) {
        try{
            method();
        }catch (Throwable a){
            System.out.println("出现异常,请检查");
            a.printStackTrace();

        }
    }
        private static void method() throws Throwable{
        int num=1/0;
            System.out.println(num);

        }

    }


在这里插入图片描述
可以抛出Throwable,因为是父类,但是catch方法也要捕捉 Throwable。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值