• 【Java】异常


    异常机制

    Java采用面向对象的方式处理异常

    • 抛出异常:在执行一个方法时若发生异常,则这个方法生成代表该异常的一个对象,停止当前执行路径,并把异常对象提交给JRE(Java Runtime Environment)
    • 捕获异常:JRE得到该异常后,寻找相应的代码来处理该异常。JRE在方法的调用栈中查找,从生成异常的方法开始回溯,直到找到相应的异常处理代码为止。

    异常分类

    继承图

    Error

    是程序无法处理的错误,较严重。

    • Virtual Machine Error
    • Out Of Memory Error

    Error发生时,Java虚拟机一般会选择线程终止。

    Exception

    是程序本身能够处理的异常。

    Runtime Exception

    这里异常通常是由编程错误导致,因此可以用逻辑处理来避免异常。

    • Null Pointer Exception
    • Array Index Out Of Bounds Exception
    • Class Cast Exception
    • Arithmetic Exception 算术异常
    • Number Format Exception

    Checked Exception

    这类异常在编译时就必须作出处理,否则无法通过编译。

    • IO Exception
    • SQL Exception

    异常的处理方式

    捕获异常

    try-catch-finally

    • \uFFFF表示空字符
    • System.out.printf不能输出char
    • 异常捕获顺序 子类在父类前面
    • finally中用来关闭程序块已打开的资源,例如关闭文件流,释放数据库连接等。
    public class IoException {
        public static void main(String[] args) {
            FileReader fileReader = null;
            try {
                fileReader = new FileReader("input/input.txt");
                char ch = (char)fileReader.read();
                while (ch != '\uFFFF') {
                    System.out.printf("" + ch);
                    ch = (char)fileReader.read();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fileReader != null)
                        fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    声明异常

    readFile抛出异常,main捕获到异常,并输出自定义语句。

    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class Throws {
        public static void main(String[] args) {
            try {
                readFile("input/input.txt");
            } catch (FileNotFoundException e) {
                System.out.println("所需文件不存在!");
            } catch (IOException e) {
                System.out.println("文件读写错误!");
            }
        }
        public static void readFile(String fileName) throws FileNotFoundException, IOException {
            FileReader fileReader = new FileReader(fileName);
            char ch = (char)fileReader.read();
            while (ch != '\uFFFF') {
                System.out.printf("" + ch);
                ch = (char)fileReader.read();
            }
            fileReader.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    自定义异常

    自定义年龄为负数的异常

    public class IllegalAgeException extends Exception{
        IllegalAgeException() {}
        IllegalAgeException(String message) {
            super(message); // 调用父类的构造函数
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    Person类

    public class Person {
        private String name;
        private int age;
        public void setName(String name) {this.name = name;};
        public void setAge(int age) throws IllegalAgeException {
            if (age < 0) {
                throw new IllegalAgeException("年龄不能为负数");
            }
            this.age = age;
        };
    
        public static void main(String[] args)  {
            Person person = new Person();
            person.setName("张三");
            try {
                person.setAge(-24);
            } catch (Exception e){
                e.printStackTrace();
            } finally {
                System.out.println("exit");
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

    IDEA 调试 debug

    • 添加断点
    • 进入调试模式
      • 编辑区单击右键,点击:debug
      • 单击工具栏上的按钮
  • 相关阅读:
    Java网络编程
    11.UDP-bite
    Hash表_拉链法_开放寻址法_模拟散列表
    Web 应用程序安全测试指南
    Redis数据库【一文教必备操作】
    Windows11不插耳机、音箱提示无法找到输出设备的问题解决方法
    iOS大厂面试查漏补缺
    FPGA:单比特跨时钟域、多比特跨时钟域(更新中)
    Spire.Office for Java 8.10.2 同步更新Crk
    sd-webui-controlnet代码分析
  • 原文地址:https://blog.csdn.net/m0_46459047/article/details/126430572