Monday, October 8, 2012

Exception

What is Exception ?
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

In java it is possible to define two categories of Exceptions and Errors.
  • JVM Exceptions: These are exceptions/errors that are exclusively or logically thrown by the JVM. Examples : NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException...
  • Programmatic exceptions: These exceptions are thrown explicitly by the application or the API programmers Examples: IllegalArgumentException, IllegalState...
What is difference between Error and Exception?  
An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. These JVM errors and you can not repair them at runtime.Though error can be caught in catch block but the execution of application will come to a halt and is not recoverable.
While exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception (probably by giving user a feedback for entering proper values etc.)

What is difference between ClassNotFoundException and NoClassDefFoundError?
A ClassNotFoundException is thrown when the reported class is not found by the ClassLoader in the CLASSPATH. It could also mean that the class in question is trying to be loaded from another class which was loaded in a parent classloader and hence the class from the child classloader is not visible. 
Consider if NoClassDefFoundError occurs which is something like
java.lang.NoClassDefFoundError
src/com/TestClass 
does not mean that the TestClass class is not in the CLASSPATH. It means that the class TestClass was found by the Class Loader however when trying to load the class, it ran into an error reading the class definition. This typically happens when the class in question has static blocks or members which use a Class that's not found by the Class Loader. So to find the culprit, view the source of the class in question (TestClass in this case) and look for code using static blocks or static members.
 

No comments:

Post a Comment