try-with-resources仅适用于AutoCloseable资源;关闭顺序为声明的逆序,异常会被压制;不可复用已有变量;不替代手动清理逻辑,需注意资源语义与副作用。
AutoCloseable 的资源Java 7 引入的 try-with-resources 不是万能语法糖,它只对声明为 AutoCloseable(或其子接口 Closeable)的类型生效。常见如 FileInputStream、BufferedReader、Connection、PreparedStatement 都实现了该接口;但像 Scanner(虽常用却未实现 AutoCloseable 直到 Java 9)、自定义类若没显式实现,就无法直接用于 try-with-resources。
public class MyResource implements AutoCloseable {
@Override
public void close() throws Exception {
// 释放逻辑,比如关闭 socket、清理 native 句柄等
}
}cannot be auto-closed; it does not implement AutoCloseable
在 try (R1 r1 = ..., R2 r2 = ..., R3 r3 = ...) 中,资源按声明**从左到右初始化**,但按**从右到左关闭**(即 r3 → r2 → r1)。若某个 close() 抛出异常,且已有主异常(如 try 块内抛出的异常),则后发生的 close 异常会被“压制”,通过 Throwable.getSuppressed() 获取。
BufferedWriter 应在 FileWriter 之后关闭,所以应写成 try (FileWriter fw = ...; BufferedWriter bw = new BufferedWriter(fw)),确保 bw 先关、fw 后关close() 中吞掉异常:否则压制机制失效,且掩盖真实问题e.printStackTrace() —— 要显式打印:for (Throwable s : e.getSuppressed()) {
System.err.println("Suppressed: " + s);
}try-with-resources 要求资源声明是**局部变量声明语句**,不能是赋值表达式或已有变量名复用。下面写法全部非法:
FileInputStream fis = new FileInputStream("a.txt"); try (fis) { ... } → 编译错误:expected resource specificationtry (var fis = existingFis)(即使 existingFis 是新创建的)→ 若 existingFis 是方法参数或字段,仍不被允许try (FileInputStream fis = new FileInputStream("a.txt")) 或 try (var fis = new FileInputStream("a.txt"))(Java 10+)它只保证 close() 被调用,但不保证关闭成功,也不处理关闭失败后的补偿行为。例如数据库连接池中的 Connection 关闭可能只是归还连接,而真正释放底层 socket 可能延后;又如某些流关闭时若写缓冲区未 flush 完,会静默丢数据。
BufferedWriter 应在 close() 前调用 flush(),或直接用 try (var bw = new BufferedWriter(...)) { bw.write(...); bw.flush(); }

AutoCloseable,它们的生命周期管理得靠其他方式(如 Spring 的 @PreDestroy)