Java : UnsupportedOperationException with Examples

UnsupportedOperationException (Java SE 17 & JDK 17) API Examples.
You will find code examples on most UnsupportedOperationException methods.


Summary

Thrown to indicate that the requested operation is not supported.

Class diagram

// List.of returns an unmodifiable list.
final var list = List.of("aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]
System.out.println(list.get(0)); // aaa

try {
    list.add("ddd");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

Constructors

UnsupportedOperationException ()

Constructs an UnsupportedOperationException with no detail message.

final var e = new UnsupportedOperationException();
System.out.println(e); // java.lang.UnsupportedOperationException

UnsupportedOperationException (String message)

Constructs an UnsupportedOperationException with the specified detail message.

final var e = new UnsupportedOperationException("abcd");
System.out.println(e); // java.lang.UnsupportedOperationException: abcd
System.out.println(e.getMessage()); // abcd

UnsupportedOperationException (String message, Throwable cause)

Constructs a new exception with the specified detail message and cause.

final var cause = new IllegalStateException("XYZ");
final var e = new UnsupportedOperationException("abcd", cause);

System.out.println(e); // java.lang.UnsupportedOperationException: abcd
System.out.println(e.getMessage()); // abcd

System.out.println(e.getCause()); // java.lang.IllegalStateException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ

UnsupportedOperationException (Throwable cause)

Constructs a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause).

final var cause = new IllegalStateException("XYZ");
final var e = new UnsupportedOperationException(cause);

System.out.println(e); // java.lang.UnsupportedOperationException: java.lang.IllegalStateException: XYZ
System.out.println(e.getMessage()); // java.lang.IllegalStateException: XYZ

System.out.println(e.getCause()); // java.lang.IllegalStateException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ

Methods declared in Throwable

addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString

Please see the link below.


Related posts

To top of page