| Author |
why this is compiling fine?
|
manu sinha
Greenhorn
Joined: Apr 29, 2005
Posts: 14
|
|
public class Test{ 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; } } } 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
author
Sheriff
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
Greenhorn
Joined: Apr 29, 2005
Posts: 14
|
|
THANK YOU
|
 |
 |
|
|
subject: why this is compiling fine?
|
|
|