Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - UnsupportedOperationException



The Java UnsupportedOperationException is thrown when an unsupported operation is attempted. The UnsupportedOperationException is a runtime exception that is thrown when an unsupported operation is attempted.

  • It is a subclass of the RuntimeException class and is part of the java.lang package.
  • It is a unchecked exception that extends the IllegalArgumentException class.
  • It is a subclass of java.lang.reflect.MalformedParameterizedTypeException.

Following are the reason when JVM throws an UnsupportedOperationException in Java:

  • When an unsupported operation is attempted, JVM throws an UnsupportedOperationException.
  • When an operation is not supported by the underlying implementation, JVM throws an UnsupportedOperationException.
  • When an operation is not supported by the underlying collection,data structure, algorithms, JVM throws an UnsupportedOperationException.

Constructors of UnsupportedOperationException

There are two constructors of UnsupportedOperationException class:

  • UnsupportedOperationException(): This constructor is used to create an UnsupportedOperationException object without any message.
  • UnsupportedOperationException(String message): This constructor is used to create an UnsupportedOperationException object with a message.
  • UnsupportedOperationException(String message, Throwable cause): This constructor is used to create an UnsupportedOperationException object with a message and the cause of the exception.
  • UnsupportedOperationException(Throwable cause): This constructor is used to create an UnsupportedOperationException object with the cause of the exception.
  • UnsupportedOperationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace): This constructor is used to create an UnsupportedOperationException object with a message, the cause of the exception, and the suppression and writable stack trace flags.
  • UnsupportedOperationException(String message, Throwable cause, boolean enableSuppression): This constructor is used to create an UnsupportedOperationException object with a message, the cause of the exception, and the suppression flag.
  • UnsupportedOperationException(String message, Throwable cause, boolean writableStackTrace): This constructor is used to create an UnsupportedOperationException object with a message, the cause of the exception, and the writable stack trace flag.
  • UnsupportedOperationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace): This constructor is used to create an UnsupportedOperationException object with a message, the cause of the exception, and the suppression and writable stack trace flags.

Methods of UnsupportedOperationException

There are some methods of UnsupportedOperationException class:

Method Description
getMessage() It is used to return the message of the exception.
toString() It is used to return the detail message string of the exception.
printStackTrace() It is used to print the stack trace of the exception.
getCause() It is used to return the cause of the exception.
initCause(Throwable cause) It is used to initialize the cause of the exception.
fillInStackTrace() It is used to fill in the execution stack trace.
printStackTrace(PrintStream s) It is used to print the stack trace of the exception to the specified print stream.
printStackTrace(PrintWriter s) It is used to print the stack trace of the exception to the specified print writer.

Hierarchy of UnsupportedOperationException

Given below is the class hierarchy of UnsupportedOperationException:

java.lang.Object
   ↳ java.lang.Throwable
       ↳ java.lang.Exception
           ↳ java.lang.RuntimeException
               ↳ java.lang.UnsupportedOperationException

Example of UnsupportedOperationException

In this example, we are trying to add an element to an unmodifiable list, so JVM will throw an UnsupportedOperationException.

import java.util.*;

public class Example {
   public static void main(String[] args) {
      List<String> list = Arrays.asList("One", "Two", "Three");
      list.add("Four"); // Throws UnsupportedOperationException
   }
}

Output

Following is the output of the above code:

Note: Example.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.base/java.util.AbstractList.add(AbstractList.java:155)
	at java.base/java.util.AbstractList.add(AbstractList.java:113)
	at Example.main(Example.java:6)

As you can see in the output, JVM throws an UnsupportedOperationException because we are trying to add an element to an unmodifiable list.

Handling UnsupportedOperationException

In this example, we are trying to add an element to an unmodifiable list, so JVM will throw an UnsupportedOperationException. We are handling this exception using try-catch block.

import java.util.*;
public class Example {
   public static void main(String[] args) {
      List list = Arrays.asList("One", "Two", "Three");
      try {
         list.add("Four"); // Throws UnsupportedOperationException
      } catch (UnsupportedOperationException e) {
         System.out.println("UnsupportedOperationException: Cannot add element to unmodifiable list!");
      }
   }
}

Output

Following is the output of the above code:

UnsupportedOperationException: Cannot add element to unmodifiable list!

As you can see in the output, we are handling the UnsupportedOperationException using try-catch block.

How to avoid UnsupportedOperationException?

There are some ways to avoid UnsupportedOperationException in Java:

  • Always check if the operation is supported by the underlying implementation.
  • Check if the collection is modifiable or not.
  • Use the Collections.unmodifiableList() method to create an unmodifiable list.
  • Use the Collections.unmodifiableSet() method to create an unmodifiable set.
  • Use the Collections.unmodifiableMap() method to create an unmodifiable map.
  • Use the Collections.unmodifiableCollection() method to create an unmodifiable collection.
  • Use the Collections.synchronizedList() method to create a synchronized list.
  • Use the Collections.synchronizedSet() method to create a synchronized set.
  • Use the Collections.synchronizedMap() method to create a synchronized map.
  • Use the Collections.synchronizedCollection() method to create a synchronized collection.
  • Use the Collections.checkedList() method to create a checked list.
  • Use the Collections.checkedSet() method to create a checked set.
  • Use the Collections.checkedMap() method to create a checked map.
  • Use the Collections.checkedCollection() method to create a checked collection.
  • Use the Collections.singletonList() method to create a singleton list.

By following the above ways, you can avoid UnsupportedOperationException in Java.

Let's see an example to avoid UnsupportedOperationException:

Example of avoiding UnsupportedOperationException

In this example, we are creating a modifiable list using the ArrayList class, so JVM will not throw an UnsupportedOperationException.

import java.util.*;
public class Example {
   public static void main(String[] args) {
      List list = new ArrayList<>(Arrays.asList("One", "Two", "Three"));
      list.add("Four"); // Works fine
      System.out.println(list);
   }
}

Output

Following is the output of the above code:

[One, Two, Three, Four]

As you can see in the output, we are creating a modifiable list using the ArrayList class and it works fine.

java_lang_exceptions.htm
Advertisements