FilterOutputStream.close() throws exception from flush()

Bernd Eckenfels ecki at
Sat May 3 00:04:32 UTC 2014


back in 2011 there was a discussion about the new changed behavior of
FilterOutputStream (and BufferedOutputStream) in regards to not anymore
swalloging IOExceptions from flush() on this list (thats where I got
the subject from).

This was generally a very good improvement (and I am glad that thereby got fixed).
However the implementation with the try-with-resource has a problem:
when flush() and close() report the same exception instance the
construction of the suppressed exception will actually fail with an

This IllegalArgumentException of Throwable.addSuppressed is very
unfortunate (it would be better simply ignore it).

Anyway, this new behavior broke a Unit-Test for Apache VFS as you can
see here:

I think this can only be fixed in Throwable by avoiding this
IllegalArgumentException or by the close() method not using

For reference, according to this changeset other locations are affected
as well:


PS: I wrote a german blogpost about the FilterOutputStream here:

The following exception is produced by the testcode:

Exception in thread "main" java.lang.IllegalArgumentException: Self-suppression not permitted
	at java.lang.Throwable.addSuppressed(
	at testfos.Main.main(
Caused by: remebered IO Exception
	at testfos.FailingOS.<init>(
	at testfos.Main.main(

package testfos;

public class Main {
    public static void main(String[] args) throws { buf = new testfos.FailingOS());
        buf.close(); // expected: IOException

package testfos;

public class FailingOS
    private ex;

    public FailingOS()
        ex = new"remebered IO Exception");

    public void write(int b) throws { }

    public void flush() throws
    { throw ex; }

    public void close() throws
    { throw ex; }

More information about the core-libs-dev mailing list