Call us on:
01392 349 580

How To Display Exceptions Through Propagation

There are many important things to remember to catch an exception. For instance one must use catch or finally to catch the thrown exceptions in try block.Also remember that try will never work without either a catch or finally.This is a must follow condition.

So what happens to a exception which is not caught by any of the catch block, of course they keep looking for its handler (its always good to catch ’em) and if they failed to find one they just propagate from one method to the other and end up crashing the program. Whenever an exception is not caught by any catch block in the method then method is said to be ducking the exception. It also means that method passes its course of work to other method to handle the exception. The exception propagation follows call stack. I think you are now grown up and must know what a call stack means. Lets rewind the stack concept that a call stack is a stack where methods are placed in LIFO order based on method call. For example if yo have called method abc() from main() then main() method will be placed at bottom of the stack and the called method abc() will be on top. After the abc finish execution the abc() will be popped from the stack and main() will take pole position.

The above call stack concept also applies to the Exception propagation. Consider we have two methods a() and b(). main() calls a() and after that a() calls method b(). Now suppose the called method b() is facing heat from JVM and it resulted in exception then the exception will look for specific handler,if it failed to either specific handler or a superclass handler then it start to look in the calling method that is a(). Here also it will follow the same procedure and if it again failed it will go to main() method and in the end if it again failed at the bottom of the stack then mate its ultimate law of nature that it has to die a horrible death,or it will produce a complete stack trace of the exception. Following figure shows how call stack woks and exception propagate”-

Make it even more clear by considering the following example:-

public class Exceptionpropagation{
public static void main(String[] a){
domore();
}
public void domore(){
doevenmore();
}
public void doevenmore(){
int x=10/0;
}
}

The produced will be in the order of stack call that is from code it is clear that the exception is generated from doevenmore() so it will be listed on top of stack trace and following the stack trace you can find out where actually the exception has happened.

The resulting exception would be and its stack trace :-
Exception in thread “main”
java. lang. ArithmeticException: / by zero
at Exceptionpropagation. doevenmore(Exceptionpropagation. java:13)
at Exceptionpropagation. domore(Exceptionpropagation. java:10)
at Exceptionpropagation. main(Exceptionpropagation. java:7)
Process Exit. . .

About the Author:
Vaibhav Pandey got offered employment from an Indian Multinational IT Company. He is 21 years old. He has a huge interest in Java programming and has liked it from his study days. Vaibhav loves to blog and share his experiences and thoughts. He now resides in Lucknow, a state capital in India. Check out his blog at http://javatutorialsworld.blogspot.com.

Get our weekly Digital Marketing Insights (its free!)

 

Discover the exact formula you need to implement to get more sales & enquiries online with our video guide.

* indicates required

Yes, I want to receive weekly digital marketing insights

* indicates required