views:

174

answers:

4

Under what circumstances would java.util.zip.ZipFile.close() throw an IOException? Its method signature indicates that it can be thrown, but from the source code there doesn't seem to be any place where this could happen, unless it's in native code. What corrective action, if any, could be taken at the point where that exception is caught?

+1  A: 

From man close(2):

Not checking the return value of close() is a common but nevertheless serious programming error. It is quite possible that errors on a previous write(2) operation are first reported at the final close(). Not checking the return value when closing the file may lead to silent loss of data. This can especially be observed with NFS and with disk quota.

Sjoerd
While closing a readonly stream can't throw, java's IO framework can't check for that as it doesn't have statically checked read vs. write streams.
Joshua
A: 

I'm not sure but I think IOException is thrown when one of the following events happen:

  • The zip file was deleted by something/someone outside of the application.
  • When the drive that contains the zip file is unmounted/disconnected

A lot more events might be the reason but those are the only two I could think of right now.

Ramon Marco Navarro
But why would either of those conditions cause an error closing a read-only ZipFile?
EJP
I didn't mention that those are the only conditions for throwing the exception.
Ramon Marco Navarro
+3  A: 

From the API docs on ZipFile.close():

Closing this ZIP file will close all of the input streams previously returned by invocations of the getInputStream method.

And InputStream.close() throws an IOException, so ZipFile.close() has to throw it too. According to the API docs for InputStream.close(), it throws an IOException "if an I/O error occurs". That's not very descriptive but it's casting a wide net. InputStreams can represent streams coming from the filesystem, network, memory, etc. InputStreams can involve buffers that need to be flushed, sockets that need to be closed, resources that need to be freed, locks that need to be freed, etc. IOExceptions can happen for a variety of reasons.

Asaph
If the exception is thrown on a read() or write() call, I can understand that the read or write failed. But if the exception is thrown on close(), what has failed?
sk
A: 

The documentation for ZipFile.close() says:

Closing this ZIP file will close all of the input streams previously returned by invocations of the getInputStream method.

Presumably the native close method is performing the close the InputStreams.

The close method of InputStream has IOException as a checked exception.

The most likely cause is an out of space condition on the filesystem where the zip file is being written error in the underlying filesystem. Unless you can identify the cause and work around it on the fly, all you can do is report the condition to the user.

Devon_C_Miller
When using an `InputStream`, you're *reading*, not *writing*, so running out of disk space due to a zip file being written couldn't be the cause of an `IOException` under the circumstances of this discussion.
Asaph
Thanks, corrected.
Devon_C_Miller