# 异常体系

# 异常的分类

  1. Exception:叫做异常,代表程序可能出现的问题,我们通常会用Exception以及他的子类来封装程序出现的问题
  2. 运行时异常:RuntimeException以及它的子类,在编译阶段不会出现异常提醒。运行时出现的异常(如:数组索引越界异常)
  3. 编译时异常:编译阶段就会出现的异常提醒。如(日期解析异常)
public class Main {
    public static void main(String[] args) {
        //异常体系

        //编译时异常模拟 (在编译阶段必须要手动处理,否则代码报错)
        String time = "2025年1月16日";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = simpleDateFormat.parse(time);
        System.out.println(date);//获取日期对象
        System.out.println(date.getTime());//获取日期时间戳
        System.out.println(simpleDateFormat.format(date.getTime()));//获取日期字符串


        //运行时异常模拟(在编译阶段是不需要处理的,是代码运行时出现的异常)
        int[] arr = {1, 2, 3, 4, 5};
        System.out.println(arr[5]);//索引越界异常
    }
}

# 异常的处理方式

# Jvm默认的处理方式

  • 把程序的异常名称,异常原因及异常出现的位置等信息输出在控制台
  • 程序停止执行,下面的代码不会执行了

# 自己处理异常 使用try catch捕获异常

格式

try{
可能出现异常的代码
} catch(异常类名 变量名){
异常的处理代码
}

目的:当代码出现异常时,可以让程序继续往下执行

  • 如果try中没有遇到问题 try中的代码会执行,catch中的代码不会执行
  • 如果try中遇到多个问题,那么就需要写多个catch与之对应
  • 如果try中遇到问题,没有被捕获,那么就会使用JVM默认的错误处理方式 下面的代码不会执行
  • 如果try中遇到问题,那么try下面的代码也不会执行
public class Main {
    public static void main(String[] args) {
        //细节:如果需要捕获多个异常,这些异常中存在父子关系的话,那么父类一定要写在下面
        int[] arr = {1, 2, 3, 4, 5};
        try {
            System.out.println("abc");
            System.out.println(arr[5]);
            System.out.println(1 / 0);

        }
        //在JDK7以后 我们可以在catch中同时捕获多个异常,中间用|进行隔开,表示如果出现了A异常或者B异常的话,采取同一种处理方案
        catch (ArithmeticException | ArrayIndexOutOfBoundsException err) {
            System.out.println("除数不能为0 或者索引越界");
        }
        //Exception 父类一定要写在最下面
        catch (Exception err) {
            System.out.println("其他异常");
        }
    }
}

# 异常中的常见方法

方法名称 说明
public String getMessage() 返回此throwable的详细消息字符串。
public String toString() 返回此可抛出的简短描述。
public void printStackTrace() 将异常错误信息输出在控制台。
class Main{
    public static void main(String[] args) {
        //异常中的常见方法
        try {
            System.out.println(1 / 0);
        } catch (ArithmeticException e) {
            //返回此错误的详细字符串
            System.out.println(e.getMessage());/// by zero
            //抛出错误的简短描述
            System.out.println(e.toString());//java.lang.ArithmeticException: / by zero
            //不会结束虚拟机
            e.printStackTrace();
        }
        System.out.println("程序继续执行");
    }
}

# 抛出处理

# throws

注意:写在方法定义处,表示声明一个异常,告诉调用者,使用本方法可能会出现哪些异常

  • 编译时异常:必须要写
  • 运行时异常:可以不写
public void 方法()throws 异常类名1,异常类名2...{
...
}

# throw

注意:写在方法内,结束方法,手动抛出异常对象,交给调用者,方法中下面的代码不会执行

public void 方法(){
 throw new 异常类名("异常原因");
}
public class Main {
    public static void main(String[] args) {
        //抛出异常
//        需求定义一个方法求数组的最大值
        int[] arr = {};
        try {
            System.out.println(getMax(arr));
        } catch (NullPointerException e) {
            System.out.println("空指针异常");
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("数组越界异常");
        }
    }
    //RuntimeException 运行时异常可以不写
    public static int getMax(int[] arr) throws RuntimeException {
        if (arr == null) {
            //手动创建一个异常对象,并把这个异常交给方法的调用者处理
            //此时方法就会结束,下面的方法不会再执行了
            //空指针异常
            throw new NullPointerException("数组为空");
        }
        if (arr.length == 0) {
            //数组越界异常 异常
            throw new ArrayIndexOutOfBoundsException("数组长度为0");
        }
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        return max;
    }
}

# 自定义异常

  1. 定义异常类(类名要见名知意)
  2. 写继承关系
  3. 空参构造 意义就是为了让控制台的报错信息更加见名知意
//技巧
//NameFormat:当前异常的名字 表示姓名格式化问题
//Exception:表示当前类是一个异常类

//运行时异常 继承RuntimeException 由于参数问题导致的异常
//编译时异常 继承Exception 核心 提醒程序员检查本地信息
public class NameFormatException  {}
package AbnormalSystem;

public class NameFormatException extends RuntimeException {
    public NameFormatException(String message) {
        super(message);
    }

    public NameFormatException() {
    }
}
上次更新: 1/17/2025, 5:31:52 PM