Java UnsupportedOperationException

Java UnsupportedOperationException

Java UnsupportedOperationException

Summary: Java UnsupportedOperationException explained in simple words for a beginner. Possibility of throwing is illustrated.

UnsupportedOperationException is thrown by the JVM at runtime. It is called when the method called is not supported by a data structure. java.lang.UnsupportedOperationException is paced in collections framework.

Following is the class signature

public class UnsupportedOperationException extends RuntimeException

Following is the hierarchy

Object –> Throwable –> Exception –> RuntimeException –> UnsupportedOperationException

Being subclass, UnsupportedOperationException can use the methods of Throwable class. The methods are getMessage() and printStackTrace() etc. One of the subclasses of this exception is HeadlessException.

Program withJava UnsupportedOperationException

Java UnsupportedOperationException
Output screen of illustrating Java UnsupportedOperationException

List cityList = new ArrayList();

A List object cityList is created. two elements are added.

List stateList = Collections.unmodifiableList(cityList);

The static method unmodifiableList(cityList) of Collections class returns an object of List. But the list returned cannot be modified. That is, elements cannot be added or removed. But it is tried to add Telangana element. The JVM throws Java UnsupportedOperationException. Observe the screenshot.

Leave a Reply

Your email address will not be published. Required fields are marked *