Monday, September 21, 2009

Exception propagation : Propagation of Uncaught exceptions

There are many important things to remember to catch an exception.Fir 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...

Suggested Reading:-

Exception Handling Basics
Exceptions: try-catch part 1
Exceptions: try-catch part 2
Use of Finally
Exception Hierarchy
User defined exceptions
Exception Matching

Sponsored Listings:-

Play free trivia and test your skill

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP