Do not ignore exceptions when closing Closeable's

Description

In many places, the method IOUtils.closeQuietly() is used to unconditionally close objects that implement the interface Closable (FileInputStreams, FileOutputStreams, ...).
Although the use of this method in finally blocks is correct and fits its purpose, Closable's should be additionally closed using the method close() within the corresponding try blocks. Otherwise, exceptions that could occur when closing a Closable will simply be ignored - possibly leading to unnoticed data corruption/loss.

Although failing on close() maybe unlikely and I do not provide the requested steps of reproduction here, its possible consequences should be absolutely avoided.

References:
See the code examples of method closeQuietly() on http://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/IOUtils.html#closeQuietly(java.io.Closeable)

Activity

Show:
Lars Kiesow
October 9, 2014, 12:12 AM

I noticed that as well and will try to fix that if I have some time left. If anyone else wants to take this ticket from me, go ahead.

Basil Brunner
November 5, 2014, 3:36 AM

As we are using Java 7 starting form 1.6 we could also think about encouraging the use of try-with-resource statements instead (http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)

Lars Kiesow
November 14, 2014, 2:53 AM

Or do something like:

try {
/* Do something with stream */
in.close();
} catch (IOException e) {
/* error handling: log error, return error response */
} finally {
IOUtils.closeQuietly(in);
}

Lars Kiesow
June 19, 2015, 4:29 AM

This has already been fixed at several places but we should go through the code one explicitly searching for this

Sven Stauber
April 6, 2016, 10:27 PM

Fixed on several places, keep an eye on this for new PRs

Fixed and reviewed

Assignee

Sven Stauber

Reporter

Sven Stauber

Tags (folksonomy)

None

Components

Fix versions

Affects versions

Priority

Major