Java异常

异常01:Error和Exception

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

异常02:捕获和抛出异常

在这里插入图片描述

try、catch用法

package com.exception.demo01;

public class Demo01 {

    public static void main(String[] args) {
        int a = 1;
        int b = 0;

        try {//监控区域:在这里面的代码,有了异常,就会捕获
            System.out.println(a / b);
        }catch (ArithmeticException e){//catch:捕获异常
            System.out.println("程序出现异常,变量b不能为0");
        }finally {//处理善后工作,无论出没出现异常,finally都会被执行
            System.out.println("finally");
        }

        //finally可以不用,但如果有IO流、与资源相关的东西需要关闭,关闭的操作一般放在finally中

    }
}

catch参数

package com.exception.demo01;

public class Demo02 {
    public void a(){
        b();
    }

    public void b(){
        a();
    }

    public static void main(String[] args) {
        try {
            new Demo02().a();
        }catch (Throwable e){ // catch中的参数是想要捕获异常的类型,最高为Throwable
            System.out.println("程序发生了异常!");
        }finally {
            System.out.println("finally");
        }
    }
}

捕获多个异常

package com.exception.demo01;

public class Demo03 {
    public void a(){
        b();
    }

    public void b(){
        a();
    }

    public static void main(String[] args) {
        //捕获多个异常时,需要有层层递进的关系(由小到大)
        try {
            new Demo03().a();
        }catch (Error e){ //捕获多个异常
            System.out.println("Error异常");
        }catch (Exception e){
            System.out.println("Exception异常");
        }catch (Throwable t){
            System.out.println("Throwable异常");
        }finally {
            System.out.println("finally");
        }

        System.out.println("====================================");

        //快捷键:选中可能会报异常的代码,按住ctrl alt t可一键生成
        int a = 1;
        int b = 0;

        try {
            System.out.println(a / b);
        } catch (Exception e) {
            System.out.println("Exception异常");
            e.printStackTrace();//打印错误的栈信息
        } finally {
            System.out.println("finally");
        }

    }
}

抛出异常

package com.exception.demo01;

public class Demo04 {

    public void test(int a, int b){
        //如果不主动抛出异常,则在调用此方法时,什么都不会报错
        //主动抛出异常时,即使不做除法操作,也可以抛出异常
        if (b == 0){
            throw new ArithmeticException();//throw 主动抛出异常,一般在方法体内使用
        }

        //System.out.println(a / b);
    }

    public static void main(String[] args) {
        new Demo04().test(1, 0);
    }
}

throws向上抛出异常

package com.exception.demo01;

public class Demo05 {

    //假设这个方法中处理不了这个异常,我们就需要把异常往外抛出去,抛到更高一级,这时在方法上抛出异常
    public void test(int a, int b) throws ArithmeticException{

        if (b == 0){
            throw new ArithmeticException();//throw 主动抛出异常,一般在方法体内使用
        }

        //System.out.println(a / b);
    }

    public static void main(String[] args) {
        //向上抛出异常时,正常情况下需要捕获
        try {
            new Demo05().test(1, 0);
        } catch (ArithmeticException e) {
            e.printStackTrace();
        }
    }
}

异常03:自定义异常及经验小结

自定义异常

在这里插入图片描述

package com.exception.demo02;

//自定义的异常类,继承Exception
public class MyException extends Exception {

    //传递数字,如果数字大于10则抛出异常
    private int detail;

    //首先写一个构造器
    public MyException(int a) {
        this.detail = a;
    }

    //打印信息,需要toString方法
    @Override
    public String toString() {
        return "MyException{" +
                "detail=" + detail +
                '}';
    }
}





//测试
package com.exception.demo02;

public class Test {

    //写一个可能会存在异常的方法
    public static void test(int a) throws MyException {

        System.out.println("传递的参数为:" + a);

        if (a > 10){
            throw new MyException(a);//抛出异常,需要在外面进行捕获
            //如果在里面捕获,则外面就不需要捕获
        }

        System.out.println("ok");
    }

    public static void main(String[] args) {
        try {
            new Test().test(15);
        } catch (MyException e) {
            //增加一些处理异常的代码块
            System.out.println(e);//e就是我们定义的toString方法
            e.printStackTrace();
        }
    }
}

小结

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值