
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
When will be an IllegalStateException (unchecked) thrown in Java?
An IllegalStateException is an unchecked exception in Java. This exception may arise in our java program mostly if we are dealing with the collection framework of java.util.package. There are many collections like List, Queue, Tree, Map out of which List and Queues (Queue and Deque) to throw this IllegalStateException at particular conditions.
When will be IllegalStateException is thrown
- An IllegalStateExceptionexception will be thrown, when we try to invoke a particular method at an inappropriate time.
- In case of java.util.List collection, we use next() method of the ListIterator interface to traverse through the java.util.List. If we call the remove() method of the ListIterator interface before calling the next() method then this exception will be thrown as it will leave the List collection in an unstable state.
- If we want to modify a particular object we will use the set() method of the ListIterator interface
- In the case of queues, if we try to add an element to a Queue, then we must ensure that the queue is not full. If this queue is full then we cannot add that element, then it will cause an IllegalStateExceptionexception to be thrown.
Example
import java.util.*; public class IllegalStateExceptionTest { public static void main(String args[]) { List list = new LinkedList(); list.add("Welcome"); list.add("to"); list.add("Tutorials"); list.add("Point"); ListIterator lIterator = list.listIterator(); lIterator.next(); lIterator.remove();// modifying the list lIterator.set("Tutorix"); System.out.println(list); } }
Output
Exception in thread "main" java.lang.IllegalStateException at java.util.LinkedList$ListItr.set(LinkedList.java:937) at IllegalStateExceptionTest.main(IllegalStateExceptionTest.java:15)
- Related Articles
- When do IllegalStateException and IllegalArgumentException get thrown? in java?
- What is a ClassCastException and when it will be thrown in Java?\n
- When does a NullPointerException get thrown in java?
- How can an exception be thrown manually by a programmer in java?
- When will be an object eligible for garbage collection?
- Java Selenium Chromedriver.exe Does not Exist IllegalStateException
- What are unchecked exceptions in Java?
- Can we throw an Unchecked Exception from a static block in java?
- How can we decide that custom exception should be checked or unchecked in java?
- Checked Vs unchecked exceptions in Java programming.
- Checked vs Unchecked exceptions in Java\n
- When will 5G services be launched in India?
- How to handle the NumberFormatException (unchecked) in Java?
- How to handle the StringIndexOutOfBoundsException (unchecked) in Java?
- Difference Between Checked and Unchecked Exception in Java

Advertisements