Document UnsupportedOperationException Error for swap function in Collections.java

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      The swap function in https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Collections.html may throw UnsupportedOperationException if called on an unmodifiable list (ex: List.of("hello", "world")). This behavior is not described in the javadoc of this function, causing a mismatch between the behavior of the function and its documentation.

      ---------- BEGIN SOURCE ----------
             List<String> unmodifiableList = List.of("hello", "world");
             try {
                  Collections.swap(unmodifiableList, 0, 1); // throw
              } catch (UnsupportedOperationException e) {
                  System.out.println("Caught UnsupportedOperationException when calling swap on unmodifiable list: " + e);
                  e.printStackTrace(System.out);
              }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Add UnsupportedOperationException in Javadoc 25.

      FREQUENCY :
      ALWAYS

            Assignee:
            Stuart Marks
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: