Java Exception Propagation

Prerequisite

Exception Propagation :  When an exception happens, propagation is the process where exception is dropped from top to bottom of stack until not caught by catch block or if no any catch block to handle it throw to JVM.

Ways to handle Exception Propagation in checked and unchecked exception are different.

  1. Exception Propagation in Unchecked Exceptions
  2. Exception Propagation in Checked Exceptions (throws)

Exception Propagation in Unchecked Exceptions

When a unchecked exception happens, Exception being dropped from to the top to the
bottom of the stack. If not caught once, the exception again drops down to the previous method and so on until it gets caught or until it reach the very bottom of the call stack. This is called exception propagation.

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

In the example below, exception occurs in method3() where it is not handled, so it is propagated to previous method2() where it is not handled, again it is propagated to method1() where exception is handled.
Exception can be handled in any method in call stack either in main() method, method1(), method2() or method3() .

public class UncheckedExceptionPropagation {
	public static void main(String[] args) {
		method1();
		System.out.println("Flow Completed..");
	}
	public static void method1()
	{
		try
		{
			method2();//Exception propagation to method1
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			System.out.println("Exception Handled Here..");
		}
	}
	public static void method2()
	{
		method3();//Exception propagation to method2
	}
	public static void method3()
	{
		int x=100/0;//ArithmeticException happen here
	}
}

Output

java.lang.ArithmeticException: / by zero
Exception Handled Here..	at com.customexceptions.UncheckedExceptionPropagation.method3(UncheckedExceptionPropagation.java:29)
	at com.customexceptions.UncheckedExceptionPropagation.method2(UncheckedExceptionPropagation.java:25)
	at com.customexceptions.UncheckedExceptionPropagation.method1(UncheckedExceptionPropagation.java:15)
	at com.customexceptions.UncheckedExceptionPropagation.main(UncheckedExceptionPropagation.java:7)

Flow Completed..

Exception Propagation in Checked Exceptions

When a checked exception happens, the propagation of exception does not happen and its mandatory to use throws keyword here. Only unchecked exceptions are propagated. Checked exceptions throw compilation error.

In example below, If we omit the throws keyword from the method3() and method2() functions, the compiler will generate compile time error. Because unlike in the case of unchecked exceptions, the checked exceptions cannot propagate without using throws keyword.

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

import java.io.IOException;

public class CheckedExceptionPropagation {
	public static void main(String[] args) {

		method1();
		System.out.println("Flow Completed..");
	}
	public static void method1()
	{
		try
		{
			method2();//Exception propagation to method1
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			System.out.println("Exception Handled Here..");
		}
	}
	public static void method2() throws IOException
	{
		method3();//Exception propagation to method2
	}
	public static void method3() throws IOException
	{
		throw new IOException("Propagation Exception test");
	}
}

Output

java.io.IOException: Propagation Exception test
	at com.customexceptions.CheckedExceptionPropagation.method3(CheckedExceptionPropagation.java:31)
	at com.customexceptions.CheckedExceptionPropagation.method2(CheckedExceptionPropagation.java:27)
	at com.customexceptions.CheckedExceptionPropagation.method1(CheckedExceptionPropagation.java:17)
	at com.customexceptions.CheckedExceptionPropagation.main(CheckedExceptionPropagation.java:9)
Exception Handled Here..
Flow Completed..

Know More

To know more about Java Exception Hierarchy, in-built exception , checked exception, unchecked exceptions and solutions. You can learn about Exception Handling in override methods and lots more. You can follow below links:  s