Java Exception propagation

Exception propagation is a way of propagating exception from method to method. Let an exception occur in a method at the top of the call stack and if it is not caught then it propagates to previous method in the call stack, if it is not caught here then it again propagates to previous method in the call stack and so on until either it is caught or reach the bottom of the stack.

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.

Program of Exception Propagation
class Test { void m(){ int data=50/0; } void n(){ m(); } void p(){ try{ n(); }catch(Exception e){System.out.println("exception handled");} } public static void main(String args[]) { Test obj=new Test(); obj.p(); System.out.println("normal flow..."); } }


Output:
exception handled normal flow...
exception propagation

In the above example exception occurs in m() method where it is not handled,so it is propagated to previous n() method where it is not handled, again it is propagated to p() method where exception is handled.

Exception can be handled in any method in call stack either in main() method,p() method,n() method or m() method.

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

Program which describes that checked exceptions are not propagated
class Test { 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[]){ Test obj=new Test(); obj.p(); System.out.println("normal flow"); } }


Output:
Compile Time Error
Example: Exception propagation in case of unchecked exception.
/** * This program used to show the use of * unchecked exception propagation. * @author codesjava */ class ArithmaticTest{ /** * This method is used to divide two integers. * @param num1 * @param num2 * @author codesjava */ public void division(int num1, int num2) { //java.lang.ArithmeticException here //and not caught hence propagate to method1. System.out.println(num1/num2); } public void method1(int num1, int num2) { //not caught here and hence propagate to method2. division(num1, num2); } public void method2(int num1, int num2){ try{ method1(num1, num2); }catch(Exception e){//caught exception here. System.out.println("Exception Handled"); } } } public class Test { public static void main(String args[]){ //creating ArithmaticTest object ArithmaticTest obj = new ArithmaticTest(); //method call obj.method2(20, 0); } }


Output:
Exception Handled
Example: Exception propagation in case of checked exception.
Note: Checked exceptions are not propagated in exception change.
import java.io.IOException; /** * This program used to show the use of * checked exception propagation. * @author codesjava */ class Test{ public void method3() { //compile time error here because //checked exceptions can't be propagated. throw new IOException(); } public void method1(){ method3(); } public void method2(){ try{ method1(); }catch(Exception e){ System.out.println("Exception Handled"); } } } public class Test1 { public static void main(String args[]){ //creating Test object Test obj = new Test(); //method call obj.method2(); } }


Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type IOException at com.codesjava.business.Test.method3 (Test1.java:13) at com.codesjava.business.Test.method1 (Test1.java:17) at com.codesjava.business.Test.method2 (Test1.java:22) at com.codesjava.business.Test1.main (Test1.java:35)



Instagram