This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Infinite loop in Exception ??  RSS feed

 
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
Boost this thread!