Java Class IllegalStateException

Overview
The IllegalStateException is an exception class in Java. The IllegalStateException class is a subclass of the RuntimeException class and it is an unchecked exception. It is automatically thrown by the Java Virtual Machine when a Java application attempts to perform an invalid operation.
For example, if you try to start a thread that has already been started, IllegalStateException would be thrown.
Introduction to Java Class IllegalStateException
There are two types of exceptions in Java: Checked and Unchecked exceptions.
- A checked exception is one that the compiler examines at compile time to ensure that the programme runs without error.
- An unchecked exception is not checked at compile-time and can occur during the runtime of a Java program without requiring the programmer to explicitly handle them.
Java IllegalStateException represents an exception that occurs when the application is in an illegal or inappropriate state to perform a certain operation. It is an unchecked exception as it is a subclass of RuntimeException.
The throws clause of a method or constructor does not need to include a declaration for the IllegalStateException because it is an unchecked exception.
Examples
Let's take a look at few examples to understand better.
It is not permitted to restart a thread that has already been started because doing so will result in a RuntimeException with the message IllegalStateException.
Example 1:
- The run() method is already running when we call the start() method.
Output:
Explanation:
- As we can see, we first built our thread in the mythread class by extending the Thread class.
- A display message is produced when the start() method is used with the intention of starting the thread.
- When start() is called again while the preceding method is still running, an exception results.
Example 2:
- When a thread has finished running the run() method, we execute the start() method on that thread.
Output:
How to solve this error?
We need to make sure that no method in our code can be called in an illegal or improper manner in order to prevent the java.lang.IllegalStateException from occurring on the main thread.
If we simply use the start() method once on thread t in the aforementioned example, IllegalStateException wouldn't be thrown.
What Causes Java Class IllegalStateException?
When the Java environment or application is not in a proper state for the requested operation, the IllegalStateException is raised. Under certain circumstances, this can happen when working with threads or the Collections framework of the java.util package. Here are several instances where this exception may apply:
- Trying to call a method that is not valid in the current state of an object. For example, attempting to close a file that has not been opened yet.
- Attempting to perform an operation that requires a certain resource that is not available. For example, trying to write to a file when the disk is full.
- Calling a method that should only be called once. For example, trying to start a thread that has already been started.
- When the Iterator interface's remove() method is used on a List without first calling the next() method. This results in a IllegalStateException and puts the List collection in an unstable condition.
- Whenever an element tries to be added to a full queue. A Java lang IllegalStateException will be raised if more elements are added than the queue can hold.
Direct Known Subclasses
A known direct relationship implies that the class is the immediate ancestor. There are various direct known subclass of IllegalStateException class. Some of them are given below:
- AlreadyBoundException
- ClosedFileSystemException
- FormatterClosedException
- WritePendingException
- InvalidMarkException
- NonWritableChannelException
and so on.
Constructors Summary of Java Class IllegalStateException
| Constructor | Description |
|---|---|
| IllegalStateException() | Creates an IllegalStateException object that has no message details. |
| IllegalStateException(String s) | Creates an IllegalStateException with the specified detail message. |
| IllegalStateException(String message, Throwable cause) | Creates a new exception with the specified detail message and the cause (Throwable instance). |
| IllegalStateException(Throwable cause) | Creates a new exception and sets its detail message to cause.toString(), which usually includes the class and message of the cause argument. |
Constructor Detail
public IllegalStateException()
- Creates an IllegalStateException instance that lacks a detail message.
- A detail message is a String that describes this particular exception.
public IllegalStateException(String s)
- Creates an IllegalStateException instance with the detail message that has been supplied.
- Parameters:
- String s is the detail message.
public IllegalStateException(String message, Throwable cause)
- Creates an IllegalStateException instance with the cause and detail message that are supplied.
- It should be noted that the detail message for this error does not automatically include the cause-related detail message.
- Parameters:
- String message is the specific information that will be stored and can be retrieved later on by calling the getMessage() method of Throwable.
- Throwable cause represents the reason behind the error or exception, which can also be saved and later retrieved by calling the getCause() method of Throwable. It is nullable.
public IllegalStateException(Throwable cause)
- Creates an IllegalStateException object with the given cause and the detail message of (cause==null? null: cause.toString()) (which typically contains the class and detail message of cause).
- For exceptions that are really wrappers for other throwables, this constructor is helpful (for example, PrivilegedActionException).
- Parameters:
- Throwable cause is a parameter that the Throwable.getCause() function saves for subsequent retrieval. (A null value is allowed; it denotes that the cause is either absent or unidentified.)
ArrayList remove() method Example
The scenario where we are using the remove() method to delete the element from the ArrayList before going to the first element is described in the following code.
Output:
The java lang IllegalStateException's fix:
We must be careful that no method in our code can be invoked at an erroneous or illegal time if we want to avoid the java.lang.IllegalStateException.
Solution
Invoke the next() method before invoking the remove() method.
Output:
Conclusion
- In Java, a runtime exception called an IllegalStateException is raised to show that a method has been called at the incorrect time. This exception serves as a warning when a method is called in an improper or unauthorised manner.
- A checked exception is one that the compiler examines to ensure that the programme runs without error at runtime.
- Unchecked exceptions are those that the compiler does not verify, regardless of whether the programmer handles them or not.
- IllegalStateException is an unchecked exception because it is a subclass of RuntimeException. When a method has been called at the incorrect time, a programmer or API developer will explicitly raise this exception to let the user know.
- When the Java environment or application is not in a proper state for the requested operation, the java langIllegalStateException is raised. Under certain circumstances, this can happen when working with threads or the Collections framework of the java.util package.