Java Exception Propagation

An exception is first thrown from the top of the stack and if it is not caught, it drops down the call stack to the previous method, If not caught there, the exception again drops down to the previous method, and so on until they are caught or until they reach the very bottom of the call stack. This is called exception propagation.


Rule: By default Unchecked Exceptions are forwarded in calling chain (propagated).


Program of Exception Propagation

Exception Propagation: Unchecked Exception

Before explaining the concept of Exception propagation, review the below code with care —

 class ExceptionPropagation {

	void method3() {

		int result = 100 / 0; // Exception Gere

	}

	void method2() {
		method3();
	}

	void method1() {
		try {
			method2();
		} catch (Exception e) {
			System.out.println("Exception is handled here");
		}
	}

	public static void main(String args[]) {
		ExceptionPropagation obj = new ExceptionPropagation();
		obj.method1();
		System.out.println("Continue with Normal Flow...");

	}

}
 

 
Exception is handled here

Continue with Normal Flow...

Explanation: Exception Propagation

When exception is occurred at the top of the stack and no exception handler is provided then exception is propagated —

exception5

We can see that

  1. Exception is occurred in the method3 () and in method3 () we don't have any exception handler.
  2. Uncaught exception will be propagated downward in stack i.e. it will check appropriate exception handler in the method2 ().
  3. Again in method2 we don't have any exception handler then again exception is propagated downward to method1() where it finds exception handler.

Thus we can see that uncaught exception is propagated in the stack until stack becomes empty, this propagation of uncaught exception is called as Exception Propagation.

Exception Propagation: Checked Exception

 class ExceptionPropagation {
	void method3() {
		throw new java.io.IOException("Checked Exception..");
	}

	void method2() {
		method3();
	}

	void method1() {
		try {
			method2();
		} catch (Exception e) {
			System.out.println("Exception is handled here");
		}
	}

	public static void main(String args[]) {
		ExceptionPropagation obj = new ExceptionPropagation();
		obj.method1();
		System.out.println("Continue with Normal Flow...");

	}

}
 

 Compile Time Error
 

You must remember one rule of thumb that − "Checked Exceptions are not propagated in the chain". Thus we will get compile error in the above case.

Guidelines on exception propagation in Java?

When do you add an exception to the method signature? For example: if an exception is only thrown when an essential program resource is missing, and can only be handled at the top level, do I propagate it through all methods using this exception through all the methods using the erring method?

Rule: By default, Checked Exceptions are not forwarded in calling chain (propagated).

Program which describes that checked exceptions are not propagated

class TestExceptionPropagation2{  
  void m(){  
    throw new java.io.IOException("device error");//checked exception  
  }  
  void n(){  
    m();  
  }  
  void p(){  
   try{  
    n();  
   }catch(Exception e){System.out.println("exception handeled");}  
  }  
  public static void main(String args[]){  
   TestExceptionPropagation2 obj=new TestExceptionPropagation2();  
   obj.p();  
   System.out.println("normal flow");  
  }  
}   

 Compile Time Error