jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes finally block executes right after the return statement? 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 block executes right after the return statement?" Watch "finally block executes right after the return statement?" New topic
Author

finally block executes right after the return statement?

Mark Lau
Ranch Hand

Joined: Dec 15, 2001
Posts: 120
On Page 238 of Sierra and Bates, they say

A finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not. Even if there is a return statement in the try block, the finally block executes right after the return statement! (Boldened by G. Chao)

And the following code is from Question 15 of Marcus Green at http://www.jchq.net/mockexams/exam1.htm , (Line number added by G. Chao)

If the file "Hello.txt" does not exist, we get this output:
No such file found, doing finally, -1
If we comment out Line 13, the output will be:
No such file found, doing finally, 0
Both cases show that the finally block executes before the return statement, not after it.
If we comment out both return statements and put a return statement right in the try block like so:

then the code won't compile at all, the compiler would say that we are missing a return statement.
So, are Sierra and Bates wrong?
Gopal Shah
Ranch Hand

Joined: May 17, 2003
Posts: 65
Originally posted by Gene Chao:
On Page 238 of Sierra and Bates, they say

If the file "Hello.txt" does not exist, we get this output:
No such file found, doing finally, -1
If we comment out Line 13, the output will be:
No such file found, doing finally, 0
Both cases show that the finally block executes before the return statement, not after it.
If we comment out both return statements and put a return statement right in the try block like so:

then the code won't compile at all, the compiler would say that we are missing a return statement.
So, are Sierra and Bates wrong?
Gopal Shah
Ranch Hand

Joined: May 17, 2003
Posts: 65
I didnt find anything wrong in the code or the output.
It seems u have misinterpreted Kathy and Sierra.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Gene -
I don't know about Sierra and Bates! jeez!
Now, if you had the Head First book, you'd see on page 309 that it sez:
If the try or catch block has a return statement, finally will still run! Flow jumps to the finally, then back to the return.

So, ouch :roll: , you got us... it's an official errata.
Anyway, good catch. The key thing for the exam is that the finally block will run regardless.
Thanks for the good catch.
-Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Mark Lau
Ranch Hand

Joined: Dec 15, 2001
Posts: 120
Hey, Bert, good to see you here. So that will appear in your next erratum?
Kashif Memon
Ranch Hand

Joined: Jul 12, 2003
Posts: 35
According to the code below Sierra and Bates are right.
import java.io.*;
public class Mine
{ public static void main(String argv[])
{ Mine m=new Mine();
System.out.println(m.amethod());
}
public int amethod()
{ try
{ FileInputStream dis=new FileInputStream("Hello.txt");
}
catch (FileNotFoundException fne)
{ System.out.println("No such file found");
return -1;
}
catch(IOException ioe)
{
}
finally
{
return 2;
}
//return 0; unreachable code
}
}
if exception is thrown
result will be
No such file found
2
In the above code return -1 gets executed first and waits for the finally to be executed because finally may have return statement.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: finally block executes right after the return statement?