This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

somebody pls explain why this code does not give an exception

 
altaf mulla
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test19{
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;


}
}
}
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't throw an exception because of the order in which the finally block is executed. The finally block is executed before the method returns. Since you return a value from finally, the method never gets to propagate that last exception.

Practically speaking, one should not return values or throw exceptions from a finally block.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic