
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - IllegalStateException
The Java IllegalStateException is thrown when an object is in an inappropriate state. The IllegalStateException is a runtime exception that is thrown when a method is invoked at an illegal or inappropriate time or if the object is in an inappropriate state.
Following are the reason when JVM throws an IllegalStateException in Java:
- When the object is in an inappropriate state, JVM throws an IllegalStateException.
- When a method is invoked at an illegal or inappropriate time, JVM throws an IllegalStateException.
Constructors of IllegalStateException
There are two constructors of IllegalStateException class:
- IllegalStateException(): This constructor is used to create an IllegalStateException object without any message.
- IllegalStateException(String message): This constructor is used to create an IllegalStateException object with a message.
Methods of IllegalStateException
There are some methods of IllegalStateException 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. |
Example of IllegalStateException
In this example, we are trying to invoke a method at an illegal or inappropriate time, so JVM will throw an IllegalStateException.
public class IllegalStateExceptionExample { public static void main(String[] args) { Thread thread = new Thread(); thread.start(); thread.start(); // This will throw IllegalStateException } }
Output
Following is the output of the above code:
Exception in thread "main" java.lang.IllegalStateException: Thread already started at java.base/java.lang.Thread.start(Thread.java:795) at IllegalStateExceptionExample.main(IllegalStateExceptionExample.java:6)
As you can see in the output, JVM throws an IllegalStateException because we are trying to start a thread that is already started.
Handling IllegalStateException
In this example, we are trying to invoke a method at an illegal or inappropriate time, so JVM will throw an IllegalStateException. We are handling this exception using try-catch block.
public class IllegalThreadStateExceptionExample { public static void main(String[] args) { Thread thread = new Thread(); try { thread.start(); // First start - Works fine thread.start(); // Second start - Throws IllegalThreadStateException } catch (IllegalThreadStateException e) { System.out.println("IllegalThreadStateException: A thread cannot be restarted once started!"); } } }
Output
Following is the output of the above code:
IllegalThreadStateException: A thread cannot be restarted once started!
How to avoid IllegalStateException?
There are some ways to avoid IllegalStateException in Java:
- Always check the state of the object before invoking a method.
- Always use a flag to check the state of the object before invoking a method.
By following the above ways, you can avoid IllegalStateException in Java.
Example 1 of avoiding IllegalStateException
Let's see an example to avoid IllegalStateException:
We will use the isAlive() method of the Thread class to check the state of the thread before starting it.
public class IllegalStateExceptionExample { public static void main(String[] args) { Thread thread = new Thread(); if(!thread.isAlive()) { thread.start(); } } }
Output
Following is the output of the above code:
There is no output of the above code because we are avoiding the IllegalStateException by checking the state of the thread before starting it.
As you can see in the output, we are checking the state of the thread before starting it, so JVM does not throw an IllegalStateException.
Example 2 of avoiding IllegalStateException
Let's see another example to avoid IllegalStateException:
We will use the getState() method of the Thread class to check the state of the thread before starting it.
public class IllegalStateExceptionExample { public static void main(String[] args) { Thread thread = new Thread(); if(thread.getState() == Thread.State.NEW) { thread.start(); } } }
Output
Following is the output of the above code:
There is no output of the above code because we are avoiding the IllegalStateException by checking the state of the thread before starting it.