Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Java reference

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 RuntimeException

Examples

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

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.

Thanks for reading!

Donate to the site — we work hard for you.