Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finally block executes right after the return statement?

 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didnt find anything wrong in the code or the output.
It seems u have misinterpreted Kathy and Sierra.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, Bert, good to see you here. So that will appear in your next erratum?
 
Kashif Memon
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic