[英]Cannot close() stream in 'finally' clause, uninitialized variable/unhandled IOException [Java IO]
So I have this method in a class file just trying to write a Book object to aPath (string).所以我在 class 文件中有这个方法,只是试图将 Book object 写入 aPath(字符串)。 However, it gives me 'Unhandled IOException', which I am not sure I understand why as it seems I have a catch clause for my try block.但是,它给了我“未处理的 IOException”,我不确定我是否理解为什么我的 try 块似乎有一个 catch 子句。
So to fix this, I tried adding a 'throws' clause to my method, however once I did that, it said that the 'out' variable was not initialized.所以为了解决这个问题,我尝试在我的方法中添加一个“抛出”子句,但是一旦我这样做了,它就说“out”变量没有被初始化。
Is there any way I can close my stream inside the finally clause?有什么办法可以在 finally 子句中关闭我的 stream 吗?
public void writeToFile(String aPath) {
ObjectOutputStream out;
try {
out = new ObjectOutputStream(new FileOutputStream(aPath));
out.writeObject(this.book);
}
catch(IOException e){
System.out.println("Error writing to file");
e.printStackTrace();
}
finally {
out.close(); //unhandled IOException error :(
}
}
Using try with resources will handle the problem of uninitialized resources: 对资源使用 try将处理未初始化资源的问题:
public void writeToFile(String aPath) {
try (ObjectOutputStream out =
new ObjectOutputStream(new FileOutputStream(aPath))) {
out.writeObject(this.book);
}
catch (IOException e) {
System.out.println("Error writing to file");
e.printStackTrace();
}
}
According to the JLS, an IOException
thrown by the writeObject
call, OR by the implicit close()
of the resource will be caught in that handler.根据 JLS, writeObject
调用或资源的隐式close()
引发的IOException
将在该处理程序中捕获。
It is so elegant...它是如此优雅...
Regarding, your attempt:关于,您的尝试:
I want to know why I cannot use out.close() in finally clause.我想知道为什么我不能在 finally 子句中使用 out.close() 。
Because out
has not necessarily been initialized.因为out
不一定已经初始化。 For example, if the IOException
was thrown in new FileOutputStream(...)
, out
won't have been initialized.例如,如果在new FileOutputStream(...)
中抛出了IOException
,则out
不会被初始化。 To get it to work using an explicit finally
, you would need to do something like this:要使用显式finally
使其工作,您需要执行以下操作:
public void writeToFile(String aPath) {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream(aPath));
out.writeObject(this.book);
}
catch (IOException e) {
System.out.println("Error opening or writing to file");
e.printStackTrace();
}
finally {
if (out != null) {
try {
out.close();
}
catch (IOException e) {
System.out.println("Error closing file");
e.printStackTrace();
}
}
}
}
Since you asked.... the exception handling / squashing is probably a bad idea:既然你问了......异常处理/挤压可能是一个坏主意:
The caller gets no indication file write has failed.调用者没有得到指示文件写入失败。 It will continue executing as if nothing happened.它将继续执行,就好像什么都没发生一样。
A stacktrace should not normally be written to stdout.通常不应将堆栈跟踪写入标准输出。 If it is relevant, the stacktrace should be logged:如果相关,则应记录堆栈跟踪:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.