FilterOutputStream.close() throws exception from flush()

Tom Hawtin tom.hawtin at oracle.com
Fri Feb 10 14:53:09 UTC 2012


On 10/02/2012 13:16, Alex Lam S.L. wrote:

> To recover the previous behaviour, I think the following might just work:
>
> try (OutputStream ostream = out) {
>    flush();
> } catch (IOException ignored) {
> }

Remember try-with-resource-catch works the inside-out from 
try-catch-finally! You are discarding the close exception. So better 
would be:

try (OutputStream ostream = out) {
     try {
         flush();
     } catch (IOException ignored) {
     }
}

It'd be kind of nice to add the flush exception to the close exception 
if there is one. I think that would require abandoning try-with-resource 
and writing it all out in longhand. Or a half-way house:

IOException flushExc = null
try (OutputStream ostream = out) {
     try {
         flush();
     } catch (IOException exc) {
         flushExc = exc;
     }
} catch (IOException exc) {
     if (flushExc != null) {
         exc.addSuppressed(flushExc);
     }
     throw exc;
}

The decorators should never have attempted to proxy the resource 
release, but it's not 1995.

Tom




More information about the core-libs-dev mailing list