
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate UnsupportedOperationException in Java
In Java, an exception is an event that occurs during the execution of a program.
When an Exception occurs, the normal flow of the program is disrupted, and the program/application terminates abnormally, which is not recommended; therefore, these exceptions can be handled.
What is UnsupportedOperationException in Java?
An UnsupportedOperationException is a subclass of the RuntimException class in Java, and it can be thrown to indicate that the requested operation is not supported.
The UnsupportedOperationException class is a member of the Java Collections Framework. This exception is thrown by almost all of the concrete collections like List, Queue, Set, and Map.
Syntax
Following is the syntax to define UnsupportedOperationException class in Java:
public class UnsupportedOperationException extends RuntimeException
How to Generate UnsupportedOperationException?
Here are a few ways to generate the UnsupportedOperationException exception in Java:
- Change or update the value of the read-only list.
- Add a new value to the unmodifiable list.
Let's implement the above approach in a Java program to generate this exception:
Example: When updating the Read-only List
In the example below, we use the UnmodifiableList() method to return an unmodifiable view of the specified list. Since we are trying to update the read-only list, which is unmodifiable, it will throw an exception:
import java.util.*; public class UnsupportedOperationExceptionTest { public static void main(String[] args) { List aList = new ArrayList(); aList.add('a'); aList.add('b'); //it will throw an UnsupportedOperationException List newList = Collections.unmodifiableList(aList); newList.add('c'); } }
The above program throws the following exception:
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055) at UnsupportedOperationExceptionTest.main(UnsupportedOperationExceptionTest.java:9)
Example: When Adding Value to the Fixed-size List
This is another example showing how to generate an UnsupportedOperationException in Java. Since we are trying to modify the fixed-size list created by the Arrays.asList() method, which throws an exception:
import java.util.Arrays; import java.util.List; public class UnsupportedOperationExceptionExample { public static void main(String[] args) { Integer array[] = {10, 20, 30}; List<Integer> list = Arrays.asList(array); list.add(40); } }
The above program throws the following exception:
Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.AbstractList.add(AbstractList.java:153) at java.base/java.util.AbstractList.add(AbstractList.java:111) at TestUnsupportedOperationException.main(TestUnsupportedOperationException.java:8)