posted 17 years ago
Hi All,
I have modified the code to look like this...
public class Test3 {
public static void main(String args[]){
method1();
System.out.println(method());
try{
throw new Exception();
}
catch(Exception e){
throw new Exception(); //A
}
finally{
}
}
public static int method(){
try{
throw new Exception();
}
catch(Exception e){
throw new Exception(); //B
}
finally{return 3;}
}
public static void method1(){
try{
throw new Exception();
}
catch(Exception e){
throw new Exception(); //C
}
finally{return;}
}
}
A:
Now as per me it is giving error in A because it does not have any method to propogate this exception to (main being at the bottom of the stack)
B & C: It wont give any error because ...
.... ""First off, we know that, by definition of the try/catch/finally statement (Section 14.19 of the JLS), the finally block is executed no matter what happens in the try/catch blocks.
Next, also by definition, both the return (Section 14.16) and throw (Section 14.17) statements complete abruptly.
Lastly, by definition, abrupt completion of a finally block (Section 14.19.2) causes the try/catch/finally statement to complete abruptly.
Therefore, executing a return or throw in the finally clause abruptly completes the finally clause which, in turn, abruptly completes the try statement which therefore has the effect of masking/overriding any previous return or throw statement executed in the associated try/catch blocks.
So, take a look at your try/catch/finally statements and check to see that you're actually handling exceptions and returns the way that you expected. " ...for more see..http://www.jguru.com/faq/view.jsp?EID=288261
Hope it helps
[ September 20, 2007: Message edited by: Abhishek khare ]
[ September 20, 2007: Message edited by: Abhishek khare ]
"Things come to those who wait, but only the things left by those who hustle."