[豪の学习笔记] JavaReStudy#10

跟学视频:韩顺平Java课程

异常

1 - 基本概念

Java语言中,将程序执行中发生的不正常情况称为“异常”(开发过程中的语法错误和逻辑错误不是异常)

执行过程中所发生的异常事件可分为两类:

①Error:Java虚拟机无法解决的严重问题,如JVM系统内部错误、资源耗尽等严重情况(StackOverflowError和OOM out of memory是严重情况,程序会崩溃)

②Exception:其他因编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理,例如空指针访问、试图读取不存在的文件、网络连接中断等。Exception又分为“运行时异常”和“编译时异常”两大类。

2 - 异常体系

Throwable作为根类,有Exception和Error两个子类

Exception类有子类RuntimeException,RuntimeException类下主要有五个常用到的子类:NullPointerException、ArithmeticException、ArrayIndexOutOfBoundsException、ClassCastException、NumberFormatException

Error类下有OutOfMemoryError和StackOverflowError两个子类

flowchart TD A[Throwable] --> B[Error] A --> C[Exception] C --> D[IOException] C --> E[ClassNotFoundException] C --> F[CloneNotSupportedException] C --> G[RuntimeException] G --> H[ArithmeticException] G --> I[ClassCastException] G --> J[IllegalArgumentException] G --> K[IllegalStateException] G --> L[IndexOutOfBoundsException] G --> M[NullPointerException] D --> N[EOFException] D --> O[FileNotFoundException] J --> P[MalformedURLException] K --> Q[UnknownHostException] style B fill:#ffcccc,stroke:#333,stroke-width:2px style C fill:#ffcccc,stroke:#333,stroke-width:2px style D fill:#ffcccc,stroke:#333,stroke-width:2px style E fill:#ffcccc,stroke:#333,stroke-width:2px style F fill:#ffcccc,stroke:#333,stroke-width:2px style G fill:#ccffcc,stroke:#333,stroke-width:2px style H fill:#ccffcc,stroke:#333,stroke-width:2px style I fill:#ccffcc,stroke:#333,stroke-width:2px style J fill:#ccffcc,stroke:#333,stroke-width:2px style K fill:#ccffcc,stroke:#333,stroke-width:2px style L fill:#ccffcc,stroke:#333,stroke-width:2px style M fill:#ccffcc,stroke:#333,stroke-width:2px style N fill:#ffcccc,stroke:#333,stroke-width:2px style O fill:#ffcccc,stroke:#333,stroke-width:2px style P fill:#ffcccc,stroke:#333,stroke-width:2px style Q fill:#ffcccc,stroke:#333,stroke-width:2px

上图红色表示编译异常、绿色表示运行异常

小结

异常分为两大类,运行时异常和编译时异常

运行时异常,编译器检查不出来,一般是编程时的逻辑错误,是程序员应该避免其出现的异常

对于运行时异常,可以不作处理,因为这类异常很普遍,若全处理可能会对程序的可读性和运行效率产生影响

编译时异常,是编译器要求必须处置的异常

3 - 常见的运行时异常

NullPointerException空指针异常

当应用程序试图在需要对象的地方使用null时,抛出该异常

ArithmeticException数学运算异常

当出现异常的运算条件时,抛出该异常

ArrayIndexOutOfBounds数组下标越界异常

用非法索引访问数组时抛出的异常,如果索引为负或大于等于数组大小则该索引为非法索引

ClassCastException类型转换异常

当试图将对象强制转换为不是实例的子类时,抛出该异常

public class ClassCastException_{
    public static void main(String[] args){
        A b = new B(); // 向上转型ok
        B b2 = (B)b; // 向下转型ok
        C c2 = (c)b; // 这里抛出ClassCastException
    }
}
class A {}
class B extends A {}
class C extends A {}

NuberFormatException数字格式不正确异常

当应用程序试图将字符串转换成一种数值类型,但该字符串不能转换为适当格式时,抛出该异常

4 - 编译异常

编译异常是指在编译期间就必须处理的异常,否则代码不能通过编译

SQLException 操作数据库时,查询表可能发生异常

IOException 操作文件时发生的异常

FileNotFoundException 当操作一个不存在的文件时,发生异常

ClassNotFoundException 加载类,而该类不存在时,发生异常

EOFException 操作文件到文件末尾,发生异常

IllegalArguementException 参数异常

5 - 异常处理

如果没有显式地进行异常处理,默认为throws

try-catch-finally

程序员在代码中捕获发生的异常,自行处理

可以有多个catch语句,捕获不同的异常(进行不同的业务处理),要求父类异常在后,子类异常在前,如果发生异常只会匹配一个catch

try{
    代码/可能有异常
} catch(Exception e){
    //捕获到异常
    //当异常发生时,系统将异常封装成Exception对象e,传递给catch
    //得到异常对象后,由程序员自行处理
    //注意,如果没有发生异常,catch代码块不会执行
}finally{
    //不管try代码块是否有异常发生,始终要执行finally,所以通常将释放资源的代码放在finally
}

throws

将发生的异常抛出,交给调用者(方法)来处理,最顶级的处理者就是JVM

如果一个方法(中的语句执行时)可能生成某种异常,但是不能确定如何处理这种异常,则此方法应显式地声明抛出异常,表明该方法将不对这些异常进行处理,而由该方法的调用者负责处理

在方法声明中用throws语句可以声明抛出异常的列表,throws后面的异常类型可以是方法中产生的异常类型,也可以是它的父类

flowchart TD JVM[JVM //处理异常 输出异常信息并退出程序] --> main[main方法 //try-catch-fin] main --> f1[f1方法 //t-c-f] f1 --> f2[f2方法 抛出异常] f2 -->|异常传递throws| main f1 -->|异常传递throws| main main -->|异常传递throws| JVM

throws注意事项和使用细节

对于编译异常,程序中必须处理,比如try-catch或者throws

对于运行时异常,程序中如果没有处理,默认就是throws的方式处理

子类重写父类的方法时,对抛出异常的规定:子类重写的方法所抛出的异常类型要么和父类抛出的异常一致,要么为父类抛出的异常的类型的子类型

在throws过程中,如果有方法try-catch,就相当于处理异常,就可以不必throws

6 - 自定义异常

①定义类:自定义异常类名,继承Exception或RuntimeException

②如果继承Exception,属于编译异常

③如果继承RuntimeException,属于运行异常(一般继承RuntimeException)

public class CustomException {
    public static void main(String[] args){
        int age = 180;
        if(!(age >= 18 && age <= 120)){
            throw new AgeException("年龄需要在18~120之间");
        }
        System.out.println("你的年龄范围正确");
    }
}
class AgeException extends RuntimeException {
    public AgeException(String message){
        super(message);
    }
}

7 - throw与throws

throws 异常处理的一种方式,位置在方法声明处,后跟异常类型

throw 手动生成异常对象的关键字,位置在方法体中,后跟异常对象

class ReturnExceptionDemo {
    static void methodA() {
        try {
            System.out.println("进入方法A");
            throw new RuntimeException(”制造异常”);
        } finally {
            System.out.println("用A方法的finally");
        }
    }
    static void methodB() {
        try {
            System.out.println("进入方法B");
            return;
        } finally {
            System.out.println("调用B方法的finally");
        }
    }
}
public static void main(String[] args){
    try{
        ReturnExceptionDemo.methodA();
    } catch (Exception e){
        System.out.println(e.getMessage());
    } finally {
        ReturnExceptionDemo.methodB();
    }
}

//输出内容:
//1.进入A方法
//2.用A方法的finally
//3.制造异常
//4.进入方法B
//5.调用B方法的finally
posted @ 2025-03-23 20:29  SchwarzShu  阅读(22)  评论(0)    收藏  举报