Exception propagation in java - deep understanding of how checked and unchecked exceptions are propagated


Contents of page :
  • Exception Propagation >
  • Propagating unchecked exception (NullPointerException) >
  • Let's see how stack of methods is formed >
  • Propagating checked exception (FileNotFoundException) using throws keyword>

Hi! In this post i’ll be explaining you in detail how checked and unchecked exceptions are propagated in java.



Exception Propagation >
Whenever methods are called stack is formed and an exception is first thrown from the top of the stack and if it is not caught, it starts coming down the stack to previous methods until it is not caught.
If exception remains uncaught even after reaching bottom of the stack it is propagated to JVM and program is terminated.


Propagating unchecked exception (NullPointerException) >
unchecked exceptions are automatically propagated in java.

Now, i’ll be explaining you how unchecked exception was propagated.
Let’s see step by step what happened in above program >
  • JVM called main method
  • step 1 - main called method1()
  • step 2 - method1 called method2()
  • step 3 - method2 called method3()
  • step 4 - method3 automatically propagated exception to method2() [because, unchecked exceptions are propagated automatically]
  • step 5 - method2 automatically propagated exception to method1() [because, unchecked exceptions are propagated automatically]
  • step 6 - method2 automatically propagated exception to main() [because, unchecked exceptions are propagated automatically]
  • main() automatically propagated exception to JVM [because, unchecked exceptions are propagated automatically]

Let's see how stack of methods is formed >

In the above program, stack is formed and an exception is first thrown from the top of the stack [ method3() ] and it remains uncaught there, and starts coming down the stack to previous methods to method2(), then to method1(), than to main() and it remains uncaught throughout.
exception remains uncaught even after reaching bottom of the stack [ main() ] so it is propagated to JVM and ultimately program is terminated by throwing exception [ as shown in output ].


Propagating checked exception (FileNotFoundException) using throws keyword >
For propagating checked exceptions method must throw exception by using throws keyword.
Now, i’ll be explaining you how checked exception was propagated.
Let’s see step by step what happened in above program >
  • JVM called main method
  • step 1 - main called method1()
  • step 2 - method1 called method2()
  • step 3 - method2 called method3()
  • step 4 - method3 propagated exception to method2() using throws keyword.[because, checked exceptions are not propagated automatically]
  • step 5 - method2 propagated exception to method1() using throws keyword.[because, checked exceptions are not propagated automatically]
  • step 6 - method2 propagated exception to main() using throws keyword.[because, checked exceptions are not propagated automatically]
  • main() propagated exception to JVM using throws keyword.[because, checked exceptions are not propagated automatically]




/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */



RELATED LINKS>


EXCEPTIONS - Top 60 interview questions and answers in java for fresher and experienced - detailed explanation with diagrams Set-1 > Q1- Q25


EXCEPTIONS - Top 60 interview questions and answers in java for fresher and experienced - 30 important OUTPUT questions Set-2 > Q26- Q60


eEdit
Must read for you :