题目 1(入门):捕获运行时异常

难度:⭐考察点:运行时异常的捕获、try-catch 基本用法要求:写一个程序,手动触发 ArrayIndexOutOfBoundsException(数组下标越界),并用 try-catch 捕获该异常,在 catch 块中打印异常信息,最后打印「程序正常结束」。

题目 2(入门):处理编译时异常

难度:⭐考察点:编译时异常的处理(throws/try-catch)要求:写一个程序,读取本地D:/test.txt文件(文件可手动创建或故意留空),处理FileNotFoundException(编译时异常):

  • 方式 1:用 try-catch 捕获异常,打印「文件不存在,请检查路径」;
  • 方式 2:用 throws 声明异常,在 main 方法中捕获。

题目 3(基础):手动抛出异常

难度:⭐⭐考察点:throw 关键字、手动抛出运行时 / 编译时异常要求:写一个「用户登录校验」方法:

  1. 接收参数:用户名(String)、密码(String);
  2. 如果用户名为 null 或空字符串,手动抛出NullPointerException(运行时异常),提示「用户名不能为空」;
  3. 如果密码长度小于 6,手动抛出IllegalArgumentException(运行时异常),提示「密码长度不能小于 6 位」;
  4. 在 main 方法中调用该方法,捕获并打印异常信息。

题目 4(基础):try-catch-finally 执行顺序

难度:⭐⭐考察点:finally 执行时机、return 与 finally 的交互要求:写程序验证以下场景的执行顺序,先预判结果,再运行验证:

java

运行

public static int testFinally() {
    try {
        System.out.println("执行try块");
        int a = 1 / 0; // 触发算术异常
        return 1;
    } catch (ArithmeticException e) {
        System.out.println("执行catch块");
        return 2;
    } finally {
        System.out.println("执行finally块");
        // 思考:如果finally中也写return 3,最终返回值是多少?
    }
}

要求

  1. 调用该方法,打印返回值;
  2. 去掉int a = 1 / 0;(不触发异常),再运行,观察执行顺序和返回值;
  3. 在 finally 中添加return 3;,再运行,分析结果。

题目 5(进阶):自定义业务异常

难度:⭐⭐⭐考察点:自定义异常、异常携带业务信息要求

  1. 自定义一个UserNotExistException(用户不存在异常),继承RuntimeException,包含「异常码(code)」和「异常信息(message)」;
  2. 写一个「根据用户 ID 查询用户」的方法:如果用户 ID 小于 1(模拟不存在),抛出UserNotExistException,异常码为 404,信息为「用户 ID 不存在」;
  3. 在 main 方法中调用该方法,捕获自定义异常,打印异常码和信息。

题目 6(综合):异常处理综合场景

难度:⭐⭐⭐考察点:多种异常的组合处理、异常传递要求:写一个程序,模拟「读取用户配置文件」的场景:

  1. 方法readConfig(String path)
    • 如果 path 为 null,抛出NullPointerException
    • 如果文件不存在,抛出FileNotFoundException(编译时异常);
    • 如果文件内容为空,抛出自定义ConfigEmptyException
  2. 在 main 方法中调用该方法,使用多层 catch(先捕获具体异常,再捕获通用异常),分别打印不同异常的提示信息;
  3. 确保无论是否发生异常,最后都打印「配置文件读取流程结束」(用 finally 实现)。

题目 1 提示

 
public class TestRuntimeException {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        try {
            // 触发数组下标越界
            System.out.println(arr[5]);
        } catch (ArrayIndexOutOfBoundsException e) {
            // 打印异常信息
            e.printStackTrace();
            System.out.println("捕获到数组下标越界异常");
        }
        System.out.println("程序正常结束");
    }
}

题目 2 提示

方式 1(try-catch):

 
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;

public class TestCheckedException {
    public static void main(String[] args) {
        File file = new File("D:/test.txt");
        try {
            FileReader fr = new FileReader(file);
        } catch (FileNotFoundException e) {
            System.out.println("文件不存在,请检查路径");
        }
    }
}

方式 2(throws):

 
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;

public class TestCheckedException2 {
    public static void readFile() throws FileNotFoundException {
        FileReader fr = new FileReader("D:/test.txt");
    }

    public static void main(String[] args) {
        try {
            readFile();
        } catch (FileNotFoundException e) {
            System.out.println("文件不存在,请检查路径");
        }
    }
}

题目 3 提示

public class TestThrow {
    public static void checkLogin(String username, String password) {
        if (username == null || username.isEmpty()) {
            throw new NullPointerException("用户名不能为空");
        }
        if (password.length() < 6) {
            throw new IllegalArgumentException("密码长度不能小于6位");
        }
        System.out.println("登录校验通过");
    }

    public static void main(String[] args) {
        try {
            checkLogin("", "12345");
        } catch (Exception e) {
            System.out.println("捕获到异常:" + e.getMessage());
        }
    }
}

题目 4 提示

  • 场景 1(触发异常):执行顺序 → try → catch → finally → 返回 2;
  • 场景 2(不触发异常):执行顺序 → try → finally → 返回 1;
  • 场景 3(finally 加 return):无论是否触发异常,最终返回 3(finally 的 return 会覆盖 try/catch 的 return)。

题目 5 提示

自定义异常类:

 
public class UserNotExistException extends RuntimeException {
    private int code;

    public UserNotExistException(int code, String message) {
        super(message);
        this.code = code;
    }

    // getter
    public int getCode() {
        return code;
    }
}

调用方法:

 
public class TestCustomException {
    public static void queryUser(int userId) {
        if (userId < 1) {
            throw new UserNotExistException(404, "用户ID不存在");
        }
        System.out.println("查询到用户:" + userId);
    }

    public static void main(String[] args) {
        try {
            queryUser(0);
        } catch (UserNotExistException e) {
            System.out.println("异常码:" + e.getCode() + ",异常信息:" + e.getMessage());
        }
    }
}

题目 6 提示

核心思路:

  1. 自定义ConfigEmptyException继承RuntimeException
  2. readConfig方法中按条件抛出不同异常;
  3. main 方法中用try-catch-catch-finally捕获(先捕获具体异常,再捕获 Exception)。
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐