1. Exception is an unwanted and unexpected event that disturbs normal flow of the program is called exception.
  2. If we are not handling exception,the program may terminate abnormally without releasing allocated resources.This is not a graceful termination.Being a good programming practice compulsory we should handle exceptions for graceful termination of the program.
  3. Exception handling doesn’t mean repairing an exception just we have to provide alternative path to continue the program normally.

Example:

try{
  //Open the PDF file from file server.
}catch(Exception ex){
  //If the PDF file is not available in file server,
  //then we use local server
}

Run time Stack Mechanism

  1. For every thread JVM provides a run time stack.
  2. All the method calls performed by that thread will be sorted in the corresponding run time stack.
  3. Once the method terminates normally, the corresponding entry from the stack will be removed.
  4. After completing all method calls the stack will become empty and JVM destroys that stack before terminating the thread.

Example:


public class ExcpetionHandDemo {

	public static void main(String[] args) {
	
		doStuff();
	}
	public static void doStuff() {
		doMoreStuff();
		
	}
	public static void doMoreStuff() {
		System.out.println("Exception thread..");
	
	}
}

RunTimeStack

Default Exception Handling

  1. When ever an exception raised by the method ,then that method is responsible for the preparation of exception object by including the following information.
    • Name of Exception.
    • Description.
    • Location of Exception.
  2. After preparation of Exception Object, The method handovers that object to the JVM.
  3. JVM will check for Exception handling code in that method, if the method doesn’t contain any exception handling code then JVM terminates that method abnormally and removes corresponding entry from the stack.
  4. JVM will check for exception handling code in the caller,and if the caller method also doesn’t contain exception handling code then JVM terminates that caller method abnormally and removes corresponding entry from the stack.
  5. This process will be continued until main method and if the main method also doesn’t contain any exception handling code then JVM terminates main method abnormally.
  6. Just before terminating the program,JVM handovers the responsibilities of exception handling to the default exception handler.
  7. Default exception handler prints the exception information to the console and terminates the program abnormally.

Example:


public class ExcpetionDemo {

	public static void main(String[] args) {
	
		doStuff();
	}
	public static void doStuff() {
		doMoreStuff();
		
	}
	public static void doMoreStuff() {
		System.out.println(10/0);
	
	}
}
/*
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at ExceptionDemo.doMoreStuff(ExceptionDemo.java:12)
	at ExceptionDemo.doStuff(ExceptionDemo.java:8)
	at ExceptionDemo.main(ExceptionDemo.java:5)
*/