aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes somebody pls explain why this code does not give an exception Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "somebody pls explain why this code does not give an exception" Watch "somebody pls explain why this code does not give an exception" New topic
Author

somebody pls explain why this code does not give an exception

altaf mulla
Greenhorn

Joined: Sep 21, 2005
Posts: 1
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

Joined: May 22, 2003
Posts: 657

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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: somebody pls explain why this code does not give an exception
 
Similar Threads
why this is compiling fine?
How to identify exception source?
exception as a argument
Object says "Who's asking?" before executing method
Exception Handling In Instance Initializer Blocks