File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Infinite loop in Exception ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Infinite loop in Exception ??" Watch "Infinite loop in Exception ??" New topic

Infinite loop in Exception ??

Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
public class Test3{
public static void main(String args[]){
public static int method(){
throw new Exception();
catch(Exception e){
throw new Exception();
return 3;

In the above code the catch block itself is throwing an Exception, so was thinking it will be an infinite loop kind off thing. I choose the answer for this as compilation error, but when I executed the o/p was 3.

Can anyone explain why ??


Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The thrown exception (from the catch block) is passed up the call stack, it does not reenter the same catch block. This is an example of what's known as "re-throwing" the exception.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: Infinite loop in Exception ??
jQuery in Action, 3rd edition