The UnSupportedOperationException
is thrown to indicate that the requested operation is not supported. This exception extends the RuntimeException
class and thus, belongs to those exceptions that can be thrown during the operation of the Java Virtual Machine (JVM). It is an unchecked exception; therefore, it does not need to be declared in a method's or a constructor's throws clause.
Lets create a list. Now use the Collections.unmodifiableList()
to make this list as unmodifiableList. After this try to add an element. We will get UnsupportedOperationException
since the list is now unmodifiableList.
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test { public static void main(String args[]) { List<String> list = new ArrayList<String>(); list.add("one"); list = Collections.unmodifiableList(list); list.add("two"); } } Output: Exception in thread "main" java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055) at Test.main(Test.java:12)
Hope you have enjoyed reading What is UnSupportedOperationException in Java?. Please do write us if you have any suggestion/comment or come across any error on this page. Thank you for reading!
Share this page on WhatsApp