Throwable
, which is base class of hierarchy.Checked Exceptions :
Unchecked Exceptions:
Whenever inside a method, if an exception has occurred, the method creates an Object known as Exception Object and hands it off to the run-time system(JVM).
The exception object contains name and description of the exception, and current state of the program where exception has occurred.
Creating the Exception Object and handling it to the run-time system is called throwing an Exception.
There might be the list of the methods that had been called to get to the method where exception was occurred.
This ordered list of the methods is called Call Stack.
Now the following procedure will happen:
The run-time system searches the call stack to find the method that contains block of code that can handle the occurred exception. The block of the code is called Exception handler.
The run-time system starts searching from the method in which exception occurred, proceeds through call stack in the reverse order in which methods were called.
If it finds appropriate handler then it passes the occurred exception to it. Appropriate handler means the type of the exception object thrown matches the type of the exception object it can handle.
If run-time system searches all the methods on call stack and couldn’t have found the appropriate handler then run-time system handover the Exception Object to default exception handler , which is part of run-time system. This handler prints the exception information in the following format and terminates program abnormally.
Exception in thread "xxx" Name of Exception : Description
... ...... .. // Call Stack
Example-1 : Handled Exception
class ExceptionThrown {
// Throws the Exception(ArithmeticException), but Appropriate Exception handler is not found here
static int divideByZero(int a, int b){
// this statement will cause ArithmeticException(/ by zero)
int i = a/b;
return i;
}
// RunTime System searches the appropriate Exception handler in this method also but couldn't have found.
// So looking forward on the call stack.
static int computeDivision(int a, int b) {
int res =0;
try {
res = divideByZero(a,b);
}catch(NumberFormatException ex){ // doesn't matches with ArithmeticException
System.out.println("NumberFormatException is occured");
}
return res;
}
// In this method found appropriate Exception handler i.e. matching catch block.
public static void main(String args[]){
int a = 1;
int b = 0;
try {
int i = computeDivision(a,b);
} catch(ArithmeticException ex) { // matching ArithmeticException
// getMessage will print description of exception(here / by zero)
System.out.println(ex.getMessage());
}
}
}
Output:
/ by zero.
Example-2 : Unhandled Exception
class ThrowsExecp {
public static void main(String args[]){
String str = null;
System.out.println(str.length());
}
}
Output:
Exception in thread "main" java.lang.NullPointerException
at ThrowsExecp.main(File.java:6)
try
, catch
, throw
, throws
, and finally
.try {
// block of code to monitor for errors the code you think can raise an exception
} catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
} catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
} finally { // optional
// block of code to be executed after try block ends
}