This is primarily a documentation and education issue.
IOUtils.closeQuietly does exactly what is says on the tin, so the method isn't defective, but I frequently see it abused to write buggy code of this form:
Search a site like stackoverflow.com for IOUtils.closeQuietly and half the examples are broken in some way. The contract for most closeable classes is that the close method can throw an IOException. By swallowing the exception, callers are not handling errors and this is rarely acceptable. In the case of output streams, this can lead to data loss.
The value of a utility library is diminished if it just provides a faster way to write bad code.
I'd like to see some advice added to the javadoc about how and when to use these methods - canonical examples wouldn't hurt.
This example code would honour the class contract:
Note: using flush is not an acceptable alternative. It is better to write to the abstraction and not the implementation and some stream types will not be able to commit all data until close is called anyway. I don't mean to lecture - I'm just trying to be thorough - this is another common piece of advice used in connection with these methods.
|Workflow||jira [ 12517466 ]||Default workflow, editable Closed status [ 12601902 ]|
|Status||Resolved [ 5 ]||Closed [ 6 ]|
|Field||Original Value||New Value|
|Status||Open [ 1 ]||Resolved [ 5 ]|
|Assignee||Niall Pemberton [ niallp ]|
|Fix Version/s||2.0 [ 12312961 ]|
|Resolution||Fixed [ 1 ]|