Sotirios-Efstathios Maneas

About Sotirios-Efstathios Maneas

Sotirios-Efstathios (Stathis) Maneas is a postgraduate student at the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. His main interests include distributed systems, web crawling, model checking, operating systems, programming languages and web applications.

java.lang.UnsupportedOperationException – How to handle UnsupportedOperationException

In this tutorial we will discuss about UnsupportedOperationException in Java. This exception 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 and thus, it does not need to be declared in a method’s or a constructor’s throws clause. Moreover, the UnsupportedOperationException exists since the 1.2 version of Java.

Finally, the UnsupportedOperationException is a member of the Java Collections Framework.

The Structure of UnsupportedOperationException

Constructors

  • UnsupportedOperationException()
  • Creates an instance of the UnsupportedOperationException class, setting null as its message.

  • UnsupportedOperationException(String s)
  • Creates an instance of the UnsupportedOperationException class, using the specified string as message. The string argument indicates the name of the class that threw the error.

  • UnsupportedOperationException(String message, Throwable cause)
  • Creates an instance of the UnsupportedOperationException class, using the specified parameters as message cause respectively.

  • UnsupportedOperationException(Throwable cause)
  • Creates an instance of the UnsupportedOperationException class, using the specified Throwable as cause. Also, the Throwable::toString() method specifies the message of the exception.

The UnsupportedOperationException in Java

The UnsupportedOperationException indicates that the requested operation cannot be performed, due to the fact that it is forbidden for that particular class. The following methods create unmodifiable views of different collections:

These views are read-only and thus, cannot be modified. If an application tries to modify such view, an UnsupportedOperationException is thrown. The following examples indicate the aforementioned cases:

UnsupportedOperationExceptionExample_Collection.java:

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;

public class UnsupportedOperationExceptionExampleCollection {
	private final static int TOTAL_ELEMS = 10;
	private final static Random random = new Random();
	
	public static void main(String[] args) {
		Collection integers = new HashSet(TOTAL_ELEMS);
		
		// Fill the collection with some random values.
		for(int i = 0; i < TOTAL_ELEMS; ++i)
			integers.add(random.nextInt());
		
		// Retrieve an unmodifiable view of the collection.
		Collection unmodifiableCollection = Collections.unmodifiableCollection(integers);
		
		// This statement throws an UnsupportedOperationException.
		unmodifiableCollection.add(random.nextInt());
	}
}

In this example, we created an instance of the HashSet class, which implements the Collection interface, and inserted a number of random values. Then, we retrieved an unmodifiable view of the Collection and tried to insert a new element, which resulted to an UnsupportedOperationException.

UnsupportedOperationExceptionExampleSet.java:

import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class UnsupportedOperationExceptionExampleSet {
	private final static int TOTAL_ELEMS = 10;
	private final static Random random = new Random();
	
	public static void main(String[] args) {
		Set integers = new HashSet(TOTAL_ELEMS);
		
		// Fill the set with some random values.
		for(int i = 0; i < TOTAL_ELEMS; ++i)
			integers.add(random.nextInt());
		
		// Retrieve an unmodifiable view of the set.
		Set unmodifiableSet = Collections.unmodifiableSet(integers);
		
		// This statement throws an UnsupportedOperationException.
		unmodifiableSet.add(random.nextInt());
	}
}

In this example, we created an instance of the HashSet class and inserted a number of random values. Then, we retrieved an unmodifiable view of the HashSet and tried to insert a new element, which resulted to an UnsupportedOperationException.

UnsupportedOperationExceptionExampleSortedSet.java:

import java.util.Collections;
import java.util.Random;
import java.util.SortedSet;
import java.util.TreeSet;

public class UnsupportedOperationExceptionExampleSortedSet {
	private final static int TOTAL_ELEMS = 10;
	private final static Random random = new Random();
	
	public static void main(String[] args) {
		TreeSet integers = new TreeSet();
		
		// Fill the tree set with some random values.
		for(int i = 0; i < TOTAL_ELEMS; ++i)
			integers.add(random.nextInt());
		
		// Retrieve an unmodifiable view of the tree set.
		SortedSet unmodifiableSortedSet = Collections.unmodifiableSortedSet(integers);
		
		// This statement throws an UnsupportedOperationException.
		unmodifiableSortedSet.add(random.nextInt());
	}
}

In this example, we created an instance of the TreeSet class and inserted a number of random values. Then, we retrieved an unmodifiable view of the TreeSet and tried to insert a new element, which resulted to an UnsupportedOperationException.

UnsupportedOperationExceptionExampleList.java:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class UnsupportedOperationExceptionExampleList {
	private final static int TOTAL_ELEMS = 10;
	private final static Random random = new Random();
	
	public static void main(String[] args) {
		List integers = new ArrayList(TOTAL_ELEMS);
		
		// Fill the list with some random values.
		for(int i = 0; i < TOTAL_ELEMS; ++i)
			integers.add(random.nextInt());
		
		// Retrieve an unmodifiable view of the list.
		List unmodifiableList = Collections.unmodifiableList(integers);
		
		// This statement throws an UnsupportedOperationException.
		unmodifiableList.add(random.nextInt());
	}
}

In this example, we created an instance of the ArrayList class and inserted a number of random values. Then, we retrieved an unmodifiable view of the ArrayList and tried to insert a new element, which resulted to an UnsupportedOperationException.

UnsupportedOperationExceptionExampleMap.java:

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class UnsupportedOperationExceptionExampleMap {
	private final static int TOTAL_ELEMS = 10;
	private final static Random random = new Random();
	
	public static void main(String[] args) {
		Map map = new HashMap();
		
		// Fill the map with some random values.
		for(int i = 0; i < TOTAL_ELEMS; ++i)
			map.put(("key_" + i), random.nextInt());
		
		// Retrieve an unmodifiable view of the map.
		Map unmodifiableMap = Collections.unmodifiableMap(map);
		
		// This statement throws an UnsupportedOperationException.
		unmodifiableMap.put("KEY", random.nextInt());
	}
}

In this example, we created an instance of the HashMap class and inserted a number of random values. Then, we retrieved an unmodifiable view of the HashMap and tried to insert a new element, which resulted to an UnsupportedOperationException.

UnsupportedOperationExceptionExampleSortedMap.java:

import java.util.Collections;
import java.util.Random;
import java.util.SortedMap;
import java.util.TreeMap;

public class UnsupportedOperationExceptionExampleSortedMap {
	private final static int TOTAL_ELEMS = 10;
	private final static Random random = new Random();
	
	public static void main(String[] args) {
		TreeMap map = new TreeMap();
		
		// Fill the tree map with some random values.
		for(int i = 0; i < TOTAL_ELEMS; ++i)
			map.put(("key_" + i), random.nextInt());
		
		// Retrieve an unmodifiable view of the tree map.
		SortedMap unmodifiableSortedMap = Collections.unmodifiableSortedMap(map);
		
		// This statement throws an UnsupportedOperationException.
		unmodifiableSortedMap.put("KEY", random.nextInt());
	}
}

In this example, we created an instance of the TreeMap class and inserted a number of random values. Then, we retrieved an unmodifiable view of the TreeMap and tried to insert a new element, which resulted to an UnsupportedOperationException.

How to deal with the UnsupportedOperationException

  • This exception is easy to deal with, because it indicates which method cannot be used. Thus, if your application requires the modification of some collection or data structures, you shall avoid using unmodifiable views.
  • Also, if this exception is thrown by a class of an external library, you shall consult its documentation, in order to understand why this particular exception is thrown.

Download the Eclipse Project

The Eclipse project of this example: UnsupportedOperationExceptionExamples.zip.

 
This was a tutorial about the UnsupportedOperationException in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close