This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Java in General and the fly likes why this is compiling fine? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "why this is compiling fine?" Watch "why this is compiling fine?" New topic

why this is compiling fine?

manu sinha

Joined: Apr 29, 2005
Posts: 14
public class Test{
public static void main(String args[]){
public static int method(){
throw new Exception();
catch(Exception e){

throw new Exception();


return 3;

This compiles fine and prints 3.Why?
Sanju Thomas
Ranch Hand

Joined: Dec 29, 2004
Posts: 243
What is wrong with this code. Even though you are throwing an Exception, Your finally block will return 3 back to the calling method. Then there is no fault with the code.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
The return in the finally block *always* gets executed, even you throw an exception earlier.

That's, of course, very bad style and actually should *never ever* be done. That is, you should never have something returned from or thrown in a finally block.

The java compiler should actually give you a warning for the above code.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
manu sinha

Joined: Apr 29, 2005
Posts: 14
It is sorta covered in the JavaRanch Style Guide.
subject: why this is compiling fine?
It's not a secret anymore!