Exceptions Handling in Java

What is an exception in Java ?
Error vs. Exception

Exception Hierarchy :

Exception-in-java1

Checked vs. Unchecked Exceptions

Exception Handling

Exception Handling by JVM :

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)

Exception Handling by Programmer :

How to handle exception ?
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
}
Important Points :
Summary :