Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when does java.util.zip.ZipFile.close() throw IOException?

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?

like image 816
sk. Avatar asked May 04 '10 15:05

sk.


2 Answers

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.

like image 144
Asaph Avatar answered Sep 28 '22 06:09

Asaph


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.

like image 40
Sjoerd Avatar answered Sep 28 '22 06:09

Sjoerd