UnsupportedOperationException
Thrown when an operation is not supported by the object, such as changing an unmodifiable collection.
Available since: Java 1.2
Syntax
public class UnsupportedOperationException extends RuntimeExceptionExamples
An unmodifiable list:
void main() {
List<String> fixed = List.of("a", "b");
try {
fixed.add("c");
} catch (UnsupportedOperationException e) {
IO.println(e);
}
List<String> resizable = Arrays.asList("x", "y");
resizable.set(0, "z");
IO.println(resizable);
}Output:
java.lang.UnsupportedOperationException
[z, y]Notes
It is thrown by collections from List.of(), Set.of(), Map.of(), stream.toList(), Collections.unmodifiableList(), and by Arrays.asList() when its size changes. The fix is a modifiable copy: new ArrayList<>(list).
See also
- List.of() — Creates an unmodifiable list from the listed elements.
- Collections.unmodifiableList() — Returns a view of a list that cannot be modified.
- Stream.toList() — Gathers stream elements into an unmodifiable list.
In the tutorial
- What exceptions are — Exceptions in Java — how a program reports an error, how to read a stack trace, common exceptions and the Throwable hierarchy.