Java exception propagation

Exception propagation in Java occurs when an exception thrown from the top of the stack. If not caught there, the exception again drops down to the previous method, and so on until caught or reach the very bottom of call stack. The list of method invocations is known as the call stack and the method of searching exception handler is called Exception Propagation.

If it don’t find exception handler till the bottom of the call stack, then exception will get handover to ‘default exception handler’.

Note that both checked and unchecked exception will get propagated if they don’t get handled in the method they raised.

Declare exception(with throws keyword) is different and Exception propagation is different. Declaration is useful at compile time to inform the caller. Exception propagation happens at runtime, It is the process of searching exception handler.

Example 01 : unchecked exception propagation – without exception declaration

Output :

main() begin
exception handled
main() end

In the above example, ArithmeticException raised in method1(). As it is not handled there then it is propagated to method2(). And we can see that method2() also not handled ArithmeticException. So it is again propagated to method3(). We can see that in method3() it got handled. This is the way exception propagation happens from the method where exception raised to the caller method.

Example 02 : unchecked exception propagation – with exception declaration

Output :

main() begin
exception handled
main() end

In the above example, we are declaring in method1() and method2() signatures saying that, there would be chance of throwing ArithmeticException when some one calls these methods.

As these methods declared unchecked exceptions there won’t be any change required in the code of caller method.

Note that ‘declaring exception’ is different and ‘exception propagation’ is different. Here whether we declare the exception or not, exception propagation happens in the same way.

Example 03 : unchecked exception propagation – default exception handler

Output:

main() begin
Exception in thread “main” java.lang.ArithmeticException: / by zero
at com.vidvaan.corejava.exception19.exception.propagation.Example03ExceptionPropagation.method1(Example03ExceptionPropagation.java:9)
at com.vidvaan.corejava.exception19.exception.propagation.Example03ExceptionPropagation.method2(Example03ExceptionPropagation.java:16)
at com.vidvaan.corejava.exception19.exception.propagation.Example03ExceptionPropagation.method3(Example03ExceptionPropagation.java:23)
at com.vidvaan.corejava.exception19.exception.propagation.Example03ExceptionPropagation.main(Example03ExceptionPropagation.java:33)

In the above example, we are declaring exception on method1(), method2(), method3() and main() method. If exception raised that will be hand over to ‘default exception handler‘.

Example 04 : checked exception propagation – no exception declaration – Compilation error

Compilation Error :

Unhandled exception type IOException

In the above example, we are getting compilation error in method1(). As it is throwing checked exception the rule is, weather we have to handle it or we need to declare that exception. In the next example we will declare checked exception.

Example 05 : checked exception propagation – with exception declaration

Output :

main() begin
exception handled
main() end

In the above example, we declared checked exception on method1(), method2() signature and we handled checked exception in method3().

So both checked and unchecked exceptions are propagated to caller method if they don’t handled. So the propagation continued until it found exception handler, If it never found exception handler it will handover exception to ‘default exception handler’.

Scroll to Top