当前位置: 欣欣网 > 码农

为什么说 与try-finally相比,首选try-with-resources

2024-08-25码农


在日常开发中,在finally进行io流的关闭,已经成了我们的编码习惯,甚至可以说是编码规范。但与 try-finally 结构相比,try-with-resources 通常是更好的选择,原因如下:

1、自动资源管理

try-with-resources 是为处理 AutoCloseable 类型的资源(如文件、数据库连接、网络套接字等)而设计的。它会在代码块执行完毕后自动关闭资源,无论代码是否正常执行还是抛出异常。这大大简化了资源管理,减少了出错的可能性。

2、更简洁的代码

使用 try-with-resources 可以减少样板代码(boilerplate code)。不需要显式地在 finally 块中调用资源的 close() 方法。相较之下,try-finally 需要手动编写关闭资源的代码,这不仅繁琐,还容易遗漏。

3、异常处理更安全

在 try-finally 中,如果 try 块中的代码和 finally 块中的 close() 方法都抛出了异常,后者的异常会掩盖前者。而 try-with-resources 能够确保 try 块抛出的异常不会被忽略,它会将关闭资源时抛出的异常作为抑制异常(Suppressed Exception)附加到原始异常中,提供更全面的错误信息。

4、防止资源泄漏

try-with-resources 的自动关闭机制可以更有效地防止资源泄漏。对于复杂的代码,如果在 try-finally 结构中忘记关闭某些资源或在关闭资源时抛出异常,可能会导致资源泄漏。而 try-with-resources 能确保资源总是被正确关闭。

5、代码示例

下面是一个使用 try-finally 和 try-with-resources 的代码示例,通过它们来读取文件内容,并在完成后关闭文件资源。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public classTryFinallyExample{
publicstaticvoidmain(String[] args){
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("example.txt"));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
catch (IOException e) {
e.printStackTrace();
finally {
// 确保资源被关闭
if (reader != null) {
try {
reader.close();
catch (IOException e) {
e.printStackTrace(); // 可能掩盖其他异常
}
}
}
}
}

使用 try-with-resources 的代码示例

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public classTryWithResourcesExample{
publicstaticvoidmain(String[] args){
// 使用 try-with-resources 自动管理资源
try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
catch (IOException e) {
e.printStackTrace();
}
// 资源会在这里自动关闭
}
}

6、对比分析

(1)try-with-resources 版本的代码更加简洁,不需要在 finally 块中显式地关闭资源。

(2)在 try-finally 版本中,finally 块中的 close() 方法可能会抛出异常,并且这个异常可能会掩盖 try 块中的异常。而在 try-with-resources 版本中,关闭资源时的异常将作为抑制异常(Suppressed Exception)被附加到原始异常中,不会丢失。

(3)try-with-resources 自动管理资源,确保资源总是在使用完毕后正确关闭,而不依赖开发者手动编写代码进行管理。这有效防止了资源泄漏的风险。

·················END·················

用官方一半价格的钱,用跟官方 ChatGPT4.0 一模一样功能的工具。

国内直接使用ChatGPT4o:

谷歌浏览器直接使用:https://www.nezhasoft.cn

  1. 无需魔法,同时支持手机、电脑

  2. 个人独享

  3. ChatGPT4o mini永久免费

  4. 支持Copilot、DALLE AI绘画、上传文件等

长按识别下方二维码,备注ai,发给你

回复gpt,获取ChatGPT4o直接使用地址

点击阅读原文,国内直接使用ChatGpt4o