java try finally 块以关闭流
我想在finally块中关闭我的流,但它抛出一个IOException
,所以看起来我必须在我的finally
中嵌套另一个try
块> 阻止以关闭流。这是正确的做法吗?看起来有点笨拙。
这是代码:
public void read() {
try {
r = new BufferedReader(new InputStreamReader(address.openStream()));
String inLine;
while ((inLine = r.readLine()) != null) {
System.out.println(inLine);
}
} catch (IOException readException) {
readException.printStackTrace();
} finally {
try {
if (r!=null) r.close();
} catch (Exception e){
e.printStackTrace();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
另外,如果您使用的是 Java 7,则可以使用 try-with-资源声明:
Also if you're using Java 7, you can use a try-with-resources statement:
这是。至少java7对资源的尝试解决了这个问题。
在 java7 之前,您可以创建一个
closeStream
函数来吞掉它:或者将 try...finally 放在 try catch 中:
它更详细,finally 中的异常会在 try 中隐藏一个异常,但它在语义上是这样的更接近 Java 中引入的 try-with-resources 7.
It is. At least java7's try with resources fixes that.
Pre java7 you can make a
closeStream
function that swallows it:Or put the try...finally inside the try catch:
It's more verbose and an exception in the finally will hide one in the try but it's semantically closer to the try-with-resources introduced in Java 7.
在 Java 7 中,您可以执行此操作...
自动关闭
。尝试阻止。
close()
。它称为尝试使用资源语句。
In Java 7 you can do this...
AutoCloseable
.try block.
close()
called when the try block exits.It's called a Try with resources statement.
是的,它笨重、丑陋且令人困惑。一种可能的解决方案是使用 Commons IO,它提供了 closeQuietly 方法。
本页右侧的“相关”栏中有许多问题实际上是重复的,我建议仔细查看这些问题以寻找处理此问题的其他方法。
Yes it is clunky, ugly and confusing. One possible solution is to use Commons IO which offers a closeQuietly method.
There's a number of questions in the "Related" column on the right hand of this page that are actually duplicates, I advise to look through these for some other ways of dealing with this issue.
就像提到 Commons IO 库的答案一样,Google Guava Libraries 有一个类似的帮助器方法对于 java.io.Closeable 的东西。该类为 com.google.common.io.Closeables。您正在寻找的函数类似于 Commons IO:closeQuietly()。
或者你可以自己动手关闭一堆,如下所示: Closeables.close(closeable1, closeable2, closeable3, ...) :
甚至返回抛出的任何异常的映射,如果没有抛出则返回 null。
Like the answer mentioning the Commons IO library, the Google Guava Libraries has a similar helper method for things which are java.io.Closeable. The class is com.google.common.io.Closeables. The function you are looking for is similarly named as Commons IO: closeQuietly().
Or you could roll your own to close a bunch like this: Closeables.close(closeable1, closeable2, closeable3, ...) :
And that even returns a map of any exceptions that were thrown or null if none were.
你的方法最终是正确的。如果您在finally 块中调用的代码可能会引发异常,请确保您要么处理它,要么记录它。永远不要让它从finally块中冒出来。
在 catch 块中,您正在吞下异常 - 这是不正确的。
谢谢...
Your approach within finally is correct. If the code that you call in a finally block can possibly throw an exception, make sure that you either handle it, or log it. Never let it bubble out of the finally block.
Within the catch block you are swallowing the exception - which is not correct.
Thanks...
源。
这个样本对我很有用。
source.
This sample was useful for me.
我在您的代码中注意到的第一件事是,如果您查看代码,则代码中缺少大括号 { } 。另外,您还需要将
r
的值初始化为null
,因此您需要首先将 null 值传递给对象,以便您编写的 if 条件可以执行not null< /code> 条件检查并允许您关闭流。
First thing I noticed in your code is curly bracket { } missing from your code if you look at it. also you need to initialize value of
r
tonull
so you need to pass null value to object at first so that if condition you have written can donot null
condition check and lets you close the stream.