学习,永无止境!|

韩熙隐ario

园龄:2年11个月粉丝:0关注:0

2025-01-15 23:50阅读: 5评论: 0推荐: 0

Java异常

什么是异常

image

image

image

image

image

异常的捕获与抛出

try catch finally的使用

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


        try {//try监控区域
            System.out.println(a / b);
        } catch (ArithmeticException e) {//catch 捕获异常,可以指定想要捕获的异常类型
            System.out.println("分母为零了");
        } finally {//善后工作
            System.out.println("finally执行了");
        }

        // finally 可以不用,一般是关闭IO,资源等工作
    }
}

可以捕获多个异常

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

        //假设要捕获多个异常:异常范围要从小到大!
        try {//try监控区域
            System.out.println(a / b);
        } catch (ArithmeticException e) {//catch 捕获异常,可以指定想要捕获的异常类型
            System.out.println("分母为零了");
        } catch (Error e) {
            System.out.println("Error");
        } catch (Throwable t) {
            System.out.println("Throwable");
        } finally {//善后工作
            System.out.println("finally执行了");
        }

    }
}

自动捕获并打印错误的栈信息

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

		//ctrl + alt + T自动报过2代码
        try {
            System.out.println(a / b);
        } catch (Exception e) {
            throw new RuntimeException(e);//打印错误的栈信息
        } finally {
        }
    }
}

主动抛出异常throw

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

    public void test(int a, int b) {
        if (b == 0) {
            throw new ArithmeticException();//主动抛出异常,一般在方法中使用
        }
        System.out.println(a / b);
    }
}

用throws关键字抛出异常,注意与throw的区分

public class Test {
    public static void main(String[] args) {
        try {
            new Test().test(1, 0);
        } catch (ArithmeticException e) {
                        System.out.println("抛出异常:ArithmeticException");

        }
    }
	//假设这方法中,处理不了这个异常,方法上抛出异常
    public void test(int a, int b) throws ArithmeticException {
        System.out.println(a / b);
    }
    //throws也可以抛出多个异常,
    //throws A, B,都让捕获的时候也要进行多个捕获
}

自定义异常

//自定义异常,具体如何实习,可以查看内置异常编写格式
public class MyException extends Exception{
    //传递数组>10
    private int detail;

    public MyException(int a) {
        this.detail = a;
    }

    //toString:异常的打印信息
    @Override
    public String toString() {
        return "MyException{"+ detail + '}';
    }
}
//调用
public class Test {
    public static void main(String[] args) {
        try {
            test(11);
        } catch (MyException e) {
            //e实际上就是异常返回值
            System.out.println("MyException=>" + e);
        }
    }
    static  void test(int a) throws MyException {
        System.out.println("a:" + a);
        if (a > 10) {
            //这里需要主动抛出,不像除数为零的自动抛出,>10本身并不是有个错误
            throw new MyException(a);
        }
        System.out.println("ok");
    }
}

总结

image

本文作者:韩熙隐ario

本文链接:https://www.cnblogs.com/arioya/p/18673926

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   韩熙隐ario  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起