The Java exception hierarchy starts from Throwable class i.e for all java Exceptions and Errors Throwable is parent class

Throwable has 2 child classes

  1. Exception:Most of the times, Exceptions are caused by due to our program code only and these are recoverable
  2. Error:Most of the times errors are raised due to lack of system resources and Errors are non-recoverable.

ExceptionHierarchy

Checked Exceptions Vs UnChecked Exceptions
  1. The Exceptions which are checked by the compiler for smooth execution of the program at run time are called checked exceptions.

    Example: FileNotFoundException, InterruptedException.

  2. The Exceptions which are unable to checked by the compiler are called unchecked exceptions.

    Example: ArithmeticException, NullPointerException.

  3. RuntimeException and it’s child classes, Error and it’s child classes are considered as unchecked exceptions and all the remaining considered as checked.
Partially checked Vs fully checked
  1. A checked exception is said to be fully checked iff all it’s child classes also checked.
    Example: IOException.
  2. A checked exception is said to be partially checked if some of it’s child classes are not checked.
    Example:Exception.

Exception Handling By Using try ,catch

We can handle the exceptions by using try catch.
We have to place the risky code inside the try block and the corresponding exception handling code inside catch block.

try{
//Risky code
}catch (X e){
//handling code
}
Without Exception handling code

public class ExceptionDemo {

	public static void main(String[] args) {

		System.out.println("Statement1");
		System.out.println(10 / 0);
		System.out.println("Statement2");
	}
}
/*
Output:
Statement1
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at ExceptionDemo.main(ExceptionDemo.java:6)

*/

Note:The above program terminates abnormally.

With Exception handling code
public class ExceptionDemo {

	public static void main(String[] args) {

		System.out.println("Statement1");
		try {
			System.out.println(10 / 0);
		} catch (ArithmeticException exception) {
			System.out.println("Exception block:" + (10 / 2));
		}
		System.out.println("Statement2");
	}
}
/*
Statement1
Exception block:5
Statement2
*/

Note:The above program terminates normally.

The Methods to display Exception Information

Throwable class contains the following methods to display error information.

Method Name Description
printStackTrace() It displays error information in the following format.
Name of Exception : Description
StackTace
toString() It displays error in the following format.
Name of Exception : Description
getMessage() It displays error information in the following format.
Description

Example:

public class ExceptionDemo {

   public static void main(String[] args) {

	try {
		System.out.println(10 / 0);
	} catch (ArithmeticException exception) {
		System.out.println("********************************************");
		exception.printStackTrace();
		System.out.println("********************************************");
	    System.out.println("exception.toString()::" + exception.toString());
		System.out.println("********************************************");
		System.out.println("exception.getMessage():"+ exception.getMessage());
		System.out.println("********************************************");

	}
   }
}
/*
Output:
********************************************
java.lang.ArithmeticException: / by zero
	at ExceptionDemo.main(ExceptionDemo.java:6)
********************************************
exception.toString()::java.lang.ArithmeticException: / by zero
********************************************
exception.getMessage():/ by zero
********************************************

*/

Note: Default Exception handler always uses printStackTrace() method only.