*
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
Author

Infinite loop in Exception ??

Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
public class Test3{
public static void main(String args[]){
System.out.println(method());
}
public static int method(){
try{
throw new Exception();
}
catch(Exception e){
throw new Exception();
}
finally{
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 ??

Thanks,
JP


Cheers,
Ram
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: http://aspose.com/file-tools
 
subject: Infinite loop in Exception ??
 
Similar Threads
Jammi Question
Exception HAndling
Doubt on thread sleep method
somebody pls explain why this code does not give an exception
why this is compiling fine?