• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
radhika holani
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Page 370- scjp1.5 K&B book

Here it is given that IllegalArgumentException and IllegalStateException are typically thrown Programmatically.
But these exceptions are runtime exceptions and so must typically be thrown by JVM.
My understanding is that ALL RuntimeExceptions are thrown by JVM.

Please explain me this concept!!!
 
Balaji VR
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First things first --> As per Java laws, All the Runtime exceptions are thrown only by JVM. What we are trying to understand is -> What makes the JVM to throw this kind of an exception?

Ok, Lets take one at a time.

(1) IllegalArgumentException :
Java docs says that -> "Thrown to indicate that a method has been passed an illegal or inappropriate argument."

What this mean is -> Programatically, when the logic tries to pass wrong argument to a method, JVM detects that during Runtime and throws this exception.

(2) IllegalStateException
Java docs states -> "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. "

What this mean is -> "The method invocation has happened (i.e the developer has written code to invoke the method during inappropriate time).

Hope this helps!
 
radhika holani
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then "ClassCastException" is also typically thrown programmatically?
 
Balaji VR
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good catch! ClassCastException.

When we understand what role Java compiler plays & what JVM does, we can easily understand this CLASSCASTEXCEPTION

Lets see what our JAVAC does. JAVAC purely does type checking. A very APT example is given in the Java doc itself!

Object x = new Integer(0);
System.out.println((String)x);

What this code does?? Merely type checking. Thats it! During complie time, the compiler does n't care what you are doing with your object. Because, during compile time, there are NO OBJECTS!!!

LETS COME TO RUNTIME NOW!!! THE JVM! This guy is responsible for all the object creation, assigning stuffs. When the above code executes, the JVM cannot cast an INTEGER object to STRING object!!

Thats why it throws the ClassCastException

Again its your mistake (I mean PROGRAMMER's mistake!). Hence Its the result of PROGRAMMATIC RUNTIME ERROR!
 
radhika holani
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx. a lot Balaji.
Got it now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic