A statement which is capable of terminating a program abruptly at runtime is known as Exception. In Java they have identified all the possible situations that are capable of terminating a program abruptly and represented them as a hierarchy of classes handled by java.lang.Throwble.
The Throwble is the super class of all the Exceptions and Errors.
An Error is a class represents the serious situation, which can not be handled programatically. Hence the programmer can not expected to catch or handle the Errors.
Hierarchy of Exception in Java:
Exceptions are broadly classified into two types
- Checked Exceptions
- Un-Checked Exceptions
Both checked and un checked Exceptions are raised at Runtime. But unchecked exceptions are not explicitly checked during the compilation of the program.
Checked Exceptions :
Checked Exceptions in Java are the Exceptions which are explicitly checked for during the compilation of the program. That is during the compilation time it self the compiler detects the presence of such exceptions and allows the program to compile if and only if the exception has been taken care by the programmer or if and only if the programmer explicitly throws the exception.
Un checked exceptions are represented by classes which are sub classes of the RuntimeExceptin. The RuntimeExceptions are thrown during the normal operations of the Java Virtual Machine.
Who Creates the Exception Object.?
When a program is submitted to JVM for execution, it starts the execution from the main method and continued till it doesn’t encounter an Exception. If it encounter an exception the JVM immediately identifies the class representing the exception, creates an object of the class and em bides it information capable of helping the programmer to over come the exception. And checks for a mechanism that is capable of overcoming the problem caused and if one is found, it handled over the control along with the object created to that mechanism. In case it doesn’t come across any such mechanism then it handled the exception in its own way by terminating the application.
In Java we have the following keywords to handle the Exceptions.