191. Java 异常 - 捕获与处理异常

191. Java 异常 - 捕获与处理异常


在 Java 中,我们通过三大组件来编写异常处理逻辑:

try:用于包裹可能抛出异常的代码块 ✅ catch:用于捕获并处理异常 ✅ finally:无论是否发生异常,都会执行的代码块(如释放资源)

从 Java SE 7 开始,Java 又引入了一个更简洁的语法糖:

try-with-resources:特别适合处理如文件、网络流等需要关闭的资源(实现了 Closeable 接口的类)


🎯 示例讲解:ListOfNumbers

我们来看一个例子,这个类的目标是:

  • 创建一个包含 0~9 的整数列表
  • 将这些数字写入一个文本文件 OutFile.txt
java 复制代码
import java.io.*;
import java.util.List;
import java.util.ArrayList;

public class ListOfNumbers {

    private List<Integer> list;
    private static final int SIZE = 10;

    public ListOfNumbers () {
        list = new ArrayList<>(SIZE);
        for (int i = 0; i < SIZE; i++) {
            list.add(i);  // 添加 0~9
        }
    }

    public void writeList() {
        // ❗ 这行会抛出 Checked Exception:IOException
        PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt"));

        for (int i = 0; i < SIZE; i++) {
            // ⚠️ 这行可能抛出 Unchecked Exception:IndexOutOfBoundsException
            out.println("Value at: " + i + " = " + list.get(i));
        }
        out.close();
    }
}

🔍 异常解释(两类异常的区别)

在这段代码中,有两种可能出错的地方:

1️⃣ new FileWriter("OutFile.txt")

  • 受检异常(Checked Exception)
  • 类型:IOException
  • 含义:可能文件不存在、没有写权限、磁盘满了等问题
  • ✅ 必须显式 try-catchthrows

2️⃣ list.get(i)

  • ⚠️ 未受检异常(Unchecked Exception)
  • 类型:IndexOutOfBoundsException
  • 含义:索引越界(比如 i = 10 时)
  • ⚠️ 可以不写 try-catch,编译器不强制要求

✅ 改进:添加异常处理

我们来修改 writeList() 方法,加入适当的异常处理逻辑:

java 复制代码
public void writeList() {
    PrintWriter out = null;
    try {
        out = new PrintWriter(new FileWriter("OutFile.txt"));  // 可能抛 IOException
        for (int i = 0; i <= SIZE; i++) {  // ❗ 有意写错,触发越界
            out.println("Value at: " + i + " = " + list.get(i));
        }
    } catch (IOException e) {
        System.err.println("无法写入文件:" + e.getMessage());
    } catch (IndexOutOfBoundsException e) {
        System.err.println("访问数组越界:" + e.getMessage());
    } finally {
        if (out != null) {
            out.close();  // 无论是否发生异常,最终关闭文件
        }
    }
}

✅ 推荐方式:使用 try-with-resources

java 复制代码
public void writeList() {
    try (PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt"))) {
        for (int i = 0; i < SIZE; i++) {
            out.println("Value at: " + i + " = " + list.get(i));
        }
    } catch (IOException e) {
        System.err.println("文件写入失败:" + e.getMessage());
    }
}

try-with-resources 的好处是:不需要手动关闭资源,系统会帮你自动调用 close(),即使发生异常也不会忘关文件。


🎓小结:

异常来源 异常类型 编译器要求处理? 推荐处理方式
new FileWriter(...) IOException ✅ 是 try-catch 或 throws
list.get(i) 超出范围 IndexOutOfBounds ❌ 否 可以加 catch(防护)
  • "写文件就像租房子,可能租不到------那就得处理!"
  • "数组越界就像走楼梯踩空了,未必每次都摔倒,但总有风险!"
  • "try-with-resources 是自动洗碗机,用了就回不去了。"
相关推荐
WebInfra4 分钟前
Rspack 2.2 发布:30+ 项性能优化,拥抱 Solid 2.0
前端·javascript·前端框架
额额额对了11 分钟前
Linux 进程管理详解:从概念到实战
java·服务器·前端
evans在进步12 分钟前
Spring Boot 启动流程与 @SpringBootApplication 核心原理详解
java·spring boot·后端
fatcoder12 分钟前
玩转 Redis · Set 篇
前端·redis·后端
爱喝麻油的小哆13 分钟前
🐾 Day 5|桌面数字人-接入llm可以对话啦
前端·three.js
我叫黑大帅17 分钟前
关于没有对生产者做校验的思考
前端·面试·架构
掘金者阿豪28 分钟前
你的公网 IP 是专线还是动态变化的?一文讲透动态 IP 与固定 IP 的那些事
前端·后端
超超不吵吵28 分钟前
Java AI转型实战(八):RAG完整链路实战
后端
invicinble37 分钟前
把握理解技术文章转化为工程水平的深度理解
前端
前端炒粉41 分钟前
fetch+readablestream(Streamable)流式输出
java·服务器·前端