aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Finally abrupt return Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Finally abrupt return " Watch "Finally abrupt return " New topic
Author

Finally abrupt return

Vikrant Sahdev
Ranch Hand

Joined: May 31, 2007
Posts: 58
I found this question on a Mock exam for SCJP1.4:




I think the answer should be A1 but its A4. I think its A1 bcos even though return exits finally block abruptly,it still compiles without error.

What do you guys think ?
Also do you think there would be any difference in answer,if compiled with 1.4 or 1.5 ?


Coming soon... www.javatales.com
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2046
It would give the same compile (error) output for both 1.4 and 1.5.

The method must have 'throws ' in its signature, because the compiler sees that it is possible that an exception will be propagated up the chain.
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
Did you try the program .I ran it ,its output is 3 .No compiler error
I ran on version 5.I dont think it should made any difference
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
It is running without adding any throws.
Swati Khanna
Greenhorn

Joined: Sep 19, 2007
Posts: 16
The above code runs without any error but if you omit the finally block then you 'll get a compiler error and the exception needs to be declared. Can somebody explain this behaviour.
Naresh Gangapur
Greenhorn

Joined: Aug 12, 2007
Posts: 11
The program compiles but with a warning: finally block doesn't complete normally . Executing a return or throw in the finally clause abruptly completes the finally clause which, in turn, abruptly completes the try statement and has the effect of masking/overriding any previous return or throw statement executed in the associated try/catch blocks.

Also, if the return statement is commented then we would have to specify the throws clause which confirms that the return statement in finally is overriding try/catch clauses.
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
When I ran above program, it compiles fine & output is 3. But when I change program like below... I moved return statement from finally to try, now it gives me compiler error "Unhandled exception" at line 1. Can any one please explain what is happening here?



SCJP-1.5<br />SCWCD-1.4
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383


Also I got unreachable statement at line 2. That I understand.
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
According to K&B you cant throw a checked exception from a catch block unless you handle AND declare it. However the orignal example in this thread seems to be voilating this rule because the method is able to throw a checked exception from the catch clause without declaring it.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

This is because there is a return statement in the finally block.

Normally, when an exception is thrown that is about to be thrown out of the method (it isn't handled by a catch-block), the finally-block is executed and then the method ends with the exception being thrown.

However, if you return from the finally-block, then you cut short the normal way of operation. Because of the return statement, the exception is swallowed and the method ends as if it returns normally. The exception does not need to be declared in a throws-clause, because it is never really thrown out of the method - the return statement prevents that.

In general, it's practically never a good idea to put a return statement inside a finally-block.
[ September 19, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
Unreachable code-Error is casued by first throw statement in try {}
2nd throw in catch produces unhandled Exception error even if there is finally block.

if finally{
return 0;} //then it runs
but if
finally{} //error

even i m not getting this behaviour
catch runs if try throws exception ,catch passes to finally.
finally doesnt handle exccpetion...actually.but why error not coming in some cases ,some cases coming
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Thanks Jesper, that explains it all
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
I tried to compile following code and it forces me to write a return statement in finally block only. why???

I don't understand the behavior. The normal convention is to write return statement in try block. Please explain.

The error is: missing return statement.


public class Test33 {

public static void main(String args[]){
System.out.println(method());
}
public static int method() {
try{
return 3;

}
catch(Exception e){
//throw new Exception();

}
finally{


}
}
}
Kumar Sushil
Greenhorn

Joined: Jan 31, 2006
Posts: 29
Code
________________________________________________________________________
public class Test33 {

public static void main(String args[]) {
System.out.println(method());
}

public static int method() {
try {
return 3;

} catch (Exception e) {
// throw new Exception();

} finally {

}
}
}
______________________________________________________________________

When, method has return type as well as try block, this is necessary that you define a catch block or finally block with reurn statement or last statement of method should be return statement. Duo to this, compiler ensure about return.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Originally posted by dolly shah:
I tried to compile following code and it forces me to write a return statement in finally block only. why???

No, it does not force you to write a return statement inside the finally block. You could also put the return statement after the finally block. Your method returns an int, so every possible path in the code that leads to the end of the method, must end in a return statement that returns an integer value.
Gunjan Kumar
Ranch Hand

Joined: Mar 26, 2007
Posts: 74
QUOTE]No, it does not force you to write a return statement inside the finally block. You could also put the return statement after the finally block. Your method returns an int, so every possible path in the code that leads to the end of the method, must end in a return statement that returns an integer value

whats the use to put return statement after finally,anyway what i feel is , that return will never get executed,




i think line 4 will never get executed , but still it executes fine and my prog ends in finally without any return.

Actually after finally , its going to end in try block , so i edited this post. Ignore my comment please
[ September 20, 2007: Message edited by: Gunjan Kumar ]

Gunjan Kumar
SCJP 1.5
Abhishek khare
Greenhorn

Joined: Aug 08, 2007
Posts: 25
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."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finally abrupt return
 
Similar Threads
Infinite loop in Exception ??
Finally abrupt return
sereral doubs... pls clear them all
Java Exception
where to use return statement