Java异常机制和 JavaSE 总结

Java异常机制和 JavaSE 总结

1.什么是异常

​ 1.1检查性异常 用户错误

​ 1.2运行时异常 代码运行的异常

​ 1.3ERROR 不时代码的问题,可能时内存问题

2.Throwable 异常的超类(error || exception)

![](https://img2023.cnblogs.com/blog/808616/202311/808616-20231101114143316-283023344.png)

​ PS: error 一般时JVM 报出的异常;exception 一般是运行异常

3.捕获和抛出异常

​ 关键字:try、catch、finally、throw、throws

public static void main(String[] args) {
    int a = 1;
    int b = 0;
    try{
        System.out.println(a/b);
    }catch (ArithmeticException e){
        System.out.println("program is exception");
    }catch (Throwable e){
        System.out.println("program is Throwable");
    }finally {
        System.out.printf("finally");
    }
}

// Ctrl + Alt + T 为 try catch 捕获  throw 来抛出异常

public void test() throws ArithmeticException{
    if(b == 0){
        throw new ArithmeticException();
    }
}

4.自定义异常

public class MyException extends Exception{

    private int detail;

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

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