When java.lang.IllegalStateException occurs in java

What is hierarchy of java.lang.IllegalStateException?

-java.lang.Object
-java.lang.Throwable
 -java.lang.Exception
  -java.lang.RuntimeException
   -java.lang.IllegalStateException



IllegalStateException is Checked (compile time exceptions) and UnChecked (RuntimeExceptions) in java ?

java.lang.IllegalStateException is a RuntimeExceptions in java.



What is IllegalStateException in java?

IllegalStateException is thrown when a method has been invoked either at illegal or inappropriate time. IllegalStateException indicates that our application is not in an appropriate state to perform requested operation.


From Effective Java -
Another commonly reused exception is IllegalStateException. This is generally the exception to throw if the invocation is illegal because of the state of the receiving object. For example, this would be the exception to throw if the caller attempted to use some object before it had been properly initialized.


Scenarios where IllegalStateException may be thrown in java>


  1. Element cannot be added in queue when its full, any attempt to do so will generate IllegalStateException.

      public boolean add(E e) {
          if (offer(e))
              return true;
          else
              throw new IllegalStateException("Queue full");
     }

  1. Calling iterator’s remove() after next() is fine, no exception is thown.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IllegalStateExceptionExample1 {
public static void main(String args[]) {
     List<String> arrayList = new ArrayList<String>();
     arrayList.add("a");
     arrayList.add("b");
     Iterator<String> it = arrayList.iterator();
     while (it.hasNext()) {
          System.out.print(it.next());
          it.remove(); // calling remove() after next() is fine
     }
}
}
/*OUTPUT
ab
*/

BUT, In the below program we will see how calling remove() without calling next() throws IllegalStateException

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IllegalStateExceptionExample2 {
public static void main(String args[]) {
     List<String> arrayList = new ArrayList<String>();
     arrayList.add("a");
     arrayList.add("b");
     Iterator<String> it = arrayList.iterator();
     while (it.hasNext()) {
       it.remove();//calling remove() without calling next() throws IllegalStateException
     }
}
}
/*OUTPUT
Exception in thread "main" java.lang.IllegalStateException
at java.util.ArrayList$Itr.remove(Unknown Source)
at IllegalStateExceptionExample2.main(IllegalStateExceptionExample2.java:14)
*/

In the above program next() method wasn’t called on iterator, so iterator’s cursor wasn’t pointing to any element, so calling remove() method throwed IllegalStateException.

  1. In addShutdownHook method  >

Once JVM’s shutdown has begun new shutdown hook cannot be registered neither  previously-registered hook can be de-registered. Any attempt made to do any of these operations causes an IllegalStateException.

  1. In setIfModifiedSince() method of java.net.URLConnection class, If already connected then throw IllegalStateException. It states that method has been invoked either at illegal or inappropriate time.
        public void setIfModifiedSince(long ifmodifiedsince) {
            if (connected)
              throw new IllegalStateException("Already connected");
            //....
}





No comments:

Post a Comment