如何简化/重用此异常处理代码
我经常写这样的代码:
BufferedWriter w = null; // Or any other object that throws exceptions and needs to be closed
try {
w = new BufferedWriter(new FileWriter(file));
// Do something with w
} catch (IOException e) {
e.printStackTrace();
} finally {
if (w != null) {
try {
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
通常涉及到一个抛出异常并需要关闭的对象,而关闭它也可能会抛出异常。
我想知道是否可以以任何方式简化或重用上述代码。
I tend to write code like the following a lot:
BufferedWriter w = null; // Or any other object that throws exceptions and needs to be closed
try {
w = new BufferedWriter(new FileWriter(file));
// Do something with w
} catch (IOException e) {
e.printStackTrace();
} finally {
if (w != null) {
try {
w.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
It usually involves an object that throws exceptions and needs to be closed, and that closing it may also throw an exception.
I was wondering if the above code can be simplified or reused in any way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您不想在finally块中编写关闭代码,您应该看看Project Lombok
更具可读性的代码,而不是编写普通的代码
使用 Lombok,您可以编写
,并且它会生成关闭 Stream 的正确方法。这适用于所有
Closeable
接口If you don't want to write code for closing in finally block, you should have a look at Project Lombok
Instead of writing the normal
With Lombok you can write
Much more readable, and it generates the correct way of closing the Stream. This works with all the
Closeable
interfaces我通常将
finally
块的内容放在助手中。就像这个Closeable 接口是由许多类(输入流、数据库连接等)实现的,所以这是一种通用的帮助器。
I usually put contents of your
finally
block in a helper. Like thisCloseable
interface is implemented by many classes (input streams, database connections, etc), so this is kinda general-purpose helper.是的,从java 1.5开始就有一个Closeable接口。您可以使用静态方法来关闭任何 Closeable 类型。
Yes, since java 1.5 there is a Closeable interface. You can have a static method that closes any Closeable type.
Java 7正在尝试资源支持。检查这个更多信息。
我在这里引用相关文本和代码示例:
使用 Java 7 中新的 try-with-resource 语言功能,您可以有效地将流参数声明为 try-construct 的一部分,并且编译器会自动生成管理这些资源的代码并为您干净利落。
Java 7 is having try with resource support. Check this out for more information.
I am quoting the relevant text and a code example here:
with the new try-with-resource language feature in Java 7, you effectively declare your stream arguments as part of the try-construct, and the compiler generates code that manages those resources automatically and cleanly for you.
我倾向于同意其他人提供的采用
Closeable
的方法,但由于维护寿命很长的程序,我使用的解决方案略有不同。基本上它需要一个OutputStream
来提供灵活性。这样做的主要优点是您可以通过多种方式调用它,从而无需专门的实用程序来处理 stderr、stdout 和文件的日志记录异常。
除了这一附加功能之外,它基本上与其他人提供的解决方案相同。
I tend to agree with others who offer a method taking a
Closeable
, but due to maintining very long lived programs, the solution I use is slightly different. Basically it takes anOutputStream
to provide flexibility.The main advantages of this is that you can call it in a variety of ways, removing the need for specialized utilities to handle logging exceptions to stderr, stdout, and files.
Other than this one added feature, it's basically the same solution others have offered.
我发现通常最好不要将 try catch 和 finally 全部放在同一个块中。通常最好有一个 try-catch 块和一个单独的 try-finally 块。
这也避免了对 w 进行空检查的需要。
I find it is usually best not to have try catch and finally all in the same block. It is often better to have a try-catch block and a separate try-finally block.
This also avoids any need to null check w.
将其写在方法中...
Write it in a method ...
可以在此处应用模板方法模式:
。
A template method pattern can be applied here:
.