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 是自动洗碗机,用了就回不去了。"
相关推荐
子兮曰1 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰1 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
前端小万1 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝1 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
胡写代码1 天前
别再前后端各写一套表单校验了
java·后端
三十而立洋1 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
大勇前进1 天前
原生 PHP 还是 Laravel?小项目到底要不要上框架
后端
yuzhi_liu1 天前
我用 LangGraph4j 实现 Multi-Agent Supervisor
后端
alsmile1 天前
Node-RED 之外,国产规则引擎的新方案:基于标准语法,Go 先行实现
后端·开源·go
大白801 天前
PHP 内存溢出排查思路:看懂报错日志,精准定位问题
后端