[9] RFR of 8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions

Peter Levart peter.levart at gmail.com
Thu Jun 25 08:31:45 UTC 2015


Hi Brian,

On 06/24/2015 09:04 PM, Brian Burkhalter wrote:
> Please review at your convenience.
>
> Issue:	https://bugs.openjdk.java.net/browse/JDK-8042377
> Patch:	http://cr.openjdk.java.net/~bpb/8042377/webrev.00/
>
> The use of try-with-resources in FilteredOutputStream.close() is replaced with explicit handling of IOExceptions potentially thrown by flush() or close(). A test covering all cases is added.
>
> Thanks,
>
> Brian

Modified code is a little different semantically from try-with 
resources. in case both flush() and out.close() throw, try-with 
resources propagates the exception thrown from flush() while modified 
code propagates exception thrown from out.close(). But that, I think, is 
actually preferable in this case, since we are calling method close() on 
FilterOutputStream.

The other semantic difference is when flush() and/or out.close() throw 
unchecked exception. In try-with-resources case, unchecked exception 
from out.close() is always added as suppressed to the exception from 
flush() while in modified code, unchecked exception from flush() is 
ignored when out.close() also throws. Also, IOExceptio from flush() is 
ignored if out.close() throws unchecked exception. Here's how both 
exceptions can be considered regardless of whether they are IOException 
or unchecked:

     public void close() throws IOException {
         if (closed.compareAndSet(false, true)) {
             Throwable flushException = null;
             try {
                 flush();
             } catch (Throwable e) {
                 flushException = e;
                 throw e;
             } finally {
                 if (flushException == null) {
                     out.close();
                 } else {
                     try {
                         out.close();
                     } catch (Throwable closeException) {
                         if (flushException != closeException) {
closeException.addSuppressed(flushException);
                         }
                         throw closeException;
                     }
                 }
             }
         }
     }


Regards, Peter




More information about the core-libs-dev mailing list