Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Infinite loop in Exception ??

 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic