Note: By default Unchecked Exceptions are forwarded in calling chain (propagated).
class ExceptionPropagation{ void m(){ int data = 10/0; } void n(){ m(); } void p(){ try{ n(); }catch(Exception e){ System.out.println("exception handled"); } } public static void main(String args[]){ ExceptionPropagation obj = new ExceptionPropagation(); obj.p(); System.out.println("normal flow..."); } }
output here:
In the following figure show the stack presentation of the exception propagation flow.
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).
class ExceptionPropagation{ 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[]){ ExceptionPropagation obj = new ExceptionPropagation(); obj.p(); System.out.println("normal flow"); } }
In the above Program which describes that checked exceptions are not propagated.
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…