- IllegalStateException vs UnsupportedOperationException
- 2 Answers 2
- Java invalid state exception
- Constructor Summary
- Method Summary
- Methods inherited from class java.lang.Throwable
- Methods inherited from class java.lang.Object
- Constructor Detail
- IllegalStateException
- IllegalStateException
- IllegalStateException
- IllegalStateException
- Java invalid state exception
- Constructor Summary
- Method Summary
- Methods declared in class java.lang.Throwable
- Methods declared in class java.lang.Object
- Constructor Detail
- IllegalStateException
- IllegalStateException
- IllegalStateException
- IllegalStateException
- Class IllegalStateException
- Constructor Summary
- Method Summary
- Methods declared in class java.lang.Throwable
- Methods declared in class java.lang.Object
- Constructor Details
- IllegalStateException
- IllegalStateException
- IllegalStateException
- IllegalStateException
IllegalStateException vs UnsupportedOperationException
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.
Thrown to indicate that the requested operation is not supported.
This is primarily opinion-based. If you only throw exception in certain illegal states, I’d vot for IllegalStateException .
@lexicore Although a question asking for when to use X and when to use Y seems to be opinion-based, there is a clear distinction between those two exceptions that can easily be explained objectively, I think.
2 Answers 2
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 < . 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 < . 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.
Java invalid state exception
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.
Constructor Summary
Constructs a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause).
Method Summary
Methods inherited from class java.lang.Throwable
Methods inherited from class java.lang.Object
Constructor Detail
IllegalStateException
public IllegalStateException()
Constructs an IllegalStateException with no detail message. A detail message is a String that describes this particular exception.
IllegalStateException
Constructs an IllegalStateException with the specified detail message. A detail message is a String that describes this particular exception.
IllegalStateException
Constructs a new exception with the specified detail message and cause. Note that the detail message associated with cause is not automatically incorporated in this exception’s detail message.
IllegalStateException
Constructs a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause). This constructor is useful for exceptions that are little more than wrappers for other throwables (for example, PrivilegedActionException ).
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
Java invalid state exception
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.
Constructor Summary
Constructs a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause ).
Method Summary
Methods declared in class java.lang.Throwable
Methods declared in class java.lang.Object
Constructor Detail
IllegalStateException
public IllegalStateException()
Constructs an IllegalStateException with no detail message. A detail message is a String that describes this particular exception.
IllegalStateException
Constructs an IllegalStateException with the specified detail message. A detail message is a String that describes this particular exception.
IllegalStateException
Constructs a new exception with the specified detail message and cause. Note that the detail message associated with cause is not automatically incorporated in this exception’s detail message.
IllegalStateException
Constructs a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause ). This constructor is useful for exceptions that are little more than wrappers for other throwables (for example, PrivilegedActionException ).
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
Class 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.
Constructor Summary
Constructs a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause ).
Method Summary
Methods declared in class java.lang.Throwable
Methods declared in class java.lang.Object
Constructor Details
IllegalStateException
Constructs an IllegalStateException with no detail message. A detail message is a String that describes this particular exception.
IllegalStateException
Constructs an IllegalStateException with the specified detail message. A detail message is a String that describes this particular exception.
IllegalStateException
Constructs a new exception with the specified detail message and cause. Note that the detail message associated with cause is not automatically incorporated in this exception’s detail message.
IllegalStateException
Constructs a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause ). This constructor is useful for exceptions that are little more than wrappers for other throwables (for example, PrivilegedActionException ).
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.