Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalStateException vs UnsupportedOperationException

I'm confusing of using exception: IllegalStateException vs UnsupportedOperationException.

I have a delete method which in some cases do not allow to use: let's say when the caller has a valid data on it.

Then I should give an exception info to user that he is doing an invalid operation now.

So, which exception should I throw? IllegalStateException or UnsupportedOperationException.

I know I can give the detail message using any of them, but I still want to know which one is better in my case.

From JavaDoc:

  • IllegalStateException:

Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.

  • UnsupportedOperationException:

Thrown to indicate that the requested operation is not supported.

like image 317
MonsterHunter Avatar asked Apr 10 '18 09:04

MonsterHunter


People also ask

When should you throw IllegalStateException?

The IllegalStateException is thrown when the Java environment or application is not in an appropriate state for the requested operation. This can occur when dealing with threads or the Collections framework of the java. util package under specific conditions.

What is the use of UnsupportedOperationException?

An UnsupportedOperationException is thrown when a requested operation cannot be performed because it is not supported for that particular class. One of the most common causes for this exception is using the asList() method of the java.

Is UnsupportedOperationException checked or unchecked?

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.

What is IllegalStateException Java?

Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.

What is an IllegalStateException?

It is thrown when a method call illegal or a method is called at incorrect time. For example, once we start a thread, we cannot restart the same thread again; if we try to do that, it throws a runtime exception i.e., IllegalStateException.

What is an example of illegalmonitorstateexception?

Here’s an example of an IllegalMonitorStateException thrown when the Iterator.remove () method is called to remove an element from an ArrayList before calling the next () method:

Why does the iterator remove () method throw an IllegalStateException?

In this example, the Iterator.remove () method throws an IllegalStateException - if the next method has not yet been called, or the remove method has already been called after the last call to the next method.

Does IllegalStateException need to be declared in the throws clause?

Since the IllegalStateException is an unchecked exception, it does not need to be declared in the throws clause of a method or constructor. The IllegalStateException is thrown when the Java environment or application is not in an appropriate state for the requested operation.


1 Answers

UnsupportedOperationException should be used as the method is not supported at all while IllegalStateException should be used as the method is supported but that in the current state, it is not legal.

The Iterator classes are good candidates to illustrate the difference between these two exceptions.

Iterator implements remove() in a default method by throwing a UnsupportedOperationException :

public interface Iterator<E> {
    ...
    default void remove() {
        throw new UnsupportedOperationException("remove");
    }
    ...
}

The method is indeed never supported by implementations that don't override this method to support it.

About implementations, we can see that the Iterator implementation used by the ArrayList class overrides remove() to support it. So UnsupportedOperationException is not thrown any longer.

On the other hand, we can also see that the method throws an IllegalStateException if you invoke it while you never invoked next() to make progress the iterator to the next element :

private class Itr implements Iterator<E> {
    ...
    public void remove() {      
        if (lastRet < 0)
            throw new IllegalStateException();
        checkForComodification();

        try {
            ArrayList.this.remove(lastRet);
            cursor = lastRet;
            lastRet = -1;
            expectedModCount = modCount;
        } catch (IndexOutOfBoundsException ex) {
            throw new ConcurrentModificationException();
        }
    ...
    }
}

The method is so supported by this implementation but if you invoke it in a illegal state, an IllegalStateException is thrown.

like image 81
davidxxx Avatar answered Oct 15 '22 07:10

davidxxx