Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-729

BrokenReader, BrokenWriter, BrokenInputStream, BrokenOutputStream can cause IllegalArgumentException in combination with try-with-resources

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.8.0
    • 2.12.0
    • Streams/Writers
    • None

    Description

      The following little code snippet can cause an IllegalArgumentException with message "Self-suppression not permitted":

              try (Writer writer = new BrokenWriter()) {
                  writer.write('a');
              }

      The try-with-resources mechanism will try to add the exception thrown from close as suppress exception to the exception thrown from write. Since those are the same, an exception is thrown. From the source of java.lang.Throwable:

          public final synchronized void addSuppressed(Throwable exception) {
              if (exception == this)
                  throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE, exception);

      Attachments

        Activity

          People

            Unassigned Unassigned
            Spoor Rob Spoor
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: