File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unreachable statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unreachable statement" Watch "Unreachable statement" New topic
Author

Unreachable statement

guillaume marchand
Greenhorn

Joined: Jan 27, 2009
Posts: 8
Hello all,

I have a question about try catch blocks and return statement.
If we consider this code :



It does not compile because the System.out.println("after"); statement is unreachable, because of the retrun; in the catch.
If the System.out.println("after"); is commented, the code compiles fine.

So my question is : why is the System.out.println("finally"); always reachable ?

Thanks in advance
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

Because it is always guaranteed that the finally block (only the finally block) will get executed even if you put a return statement in either of the try or catch blocks or both. So what all you put into the finally block will be considered as a reachable code.


SCJP 5.0 and now fighting with ExtJS
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

BUt if there is a System.exit(); in try or catch block then finally block will not be executed.


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Hs Arun
Greenhorn

Joined: May 01, 2008
Posts: 14
Its the design aspect of JVM, Whenever control enters inside any try block then the code inside finally block(if any for that try block) will get executed for sure, whether exception's thrown or not inside that try block.

We can use catch block to increase the availability of our application (for eg., lets say exception is thrown in middle of some costly sequence of operation and if we dont expect users to repeat all the previous steps then we can write code inside catch to increase availability of our app)

We can use finally to release any resources (if any acquired inside the try block).

Rgds,
Arun
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Because Java Language Specification says that:
If a finally block is present, it is reachable iff the try statement is reachable


In this case (after commenting "after"), finally will be executed (and print "finally") so it is quite obvious that it is reachable.
guillaume marchand
Greenhorn

Joined: Jan 27, 2009
Posts: 8
Ok ! Thanks for those explanations.
Lourenço Marinho
Greenhorn

Joined: Feb 02, 2009
Posts: 1
but after the finally block execution, does it goes back to the catch block and the retrun statement is executed?
let´s suppose there was a return statement in the finally block, which return statement will be excuted?

thanks.
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

It won't go back and execute the catch blocks' return statement. If try, catch and finally has return statements then the return statement of the finally block will be executed.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Balagopal Kannampallil wrote:It won't go back and execute the catch blocks' return statement. If try, catch and finally has return statements then the return statement of the finally block will be executed.

But what if the finally block doesn't have a return statement?


All code in my posts, unless a source is explicitly mentioned, is my own.
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

After entering the finally block, the code inside the finally block get executed and if the finally block does not have a return statement then it will execute the return statement of whatever condition it satisfied before it entered the finally block (either try or catch).
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Balagopal Kannampallil wrote:After entering the finally block, the code inside the finally block get executed and if the finally block does not have a return statement then it will execute the return statement of whatever condition it satisfied before it entered the finally block (either try or catch).

You're right, thank you.
nitude gupta
Ranch Hand

Joined: Sep 04, 2008
Posts: 34
Just a query in continuation to previous discussion, Suppose if try is having return statement and catch and finally don't.

An exception is thrown before code reaches return of try,will it still execute return of try
Balaji Bang
Ranch Hand

Joined: Apr 23, 2007
Posts: 180
Hi gupta you are asking about this situation :



This gives Compiler Error at Line 1


My Doubt is

This is not giving any Compiler error.

But why the following is giving Error ???



This is also giving Error. what is the logic in this??? If there is a return only in finally it is giving error. if there is return in only catch it is giving error. But if there is return only in try it is not giving error. Why. Really I am in Confusion. I got this doubt earlier but didn't post it here.


Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

If the try is having a return statement and the corresponding catch and finally does not have a return statement then the method must have a default return statement at the end outside these try-catch-finally blocks (if the return type of the method is not void).
So when an excepion occurs before the return statement in the try block,the exception is caught and handled by the catch block, then the code inside the finally block gets executed and at last the default return at the end of the method will get executed and this value is retuned.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Balagopal Kannampallil wrote:If the try is having a return statement and the corresponding catch and finally does not have a return statement then the method must have a default return statement at the end outside these try-catch-finally blocks (if the return type of the method is not void).
So when an excepion occurs before the return statement in the try block,the exception is caught and handled by the catch block, then the code inside the finally block gets executed and at last the default return at the end of the method will get executed and this value is retuned.

Actually, if you get to the finally block, the default return (or any other code) following the try/catch/finally construct will never be executed.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Banu Chowdary wrote:Hi gupta you are asking about this situation :




This gives Compiler Error at Line 1 because that statement will be unreachable due to the exception thrown before it. It doesn't matter whether the statement at line 1 is a return statement or anything else, it will be an unreachable statement and the compiler will be able to see that and issue an error.
Balagopal Kannampallil
Ranch Hand

Joined: Oct 18, 2004
Posts: 136

Balagopal Kannampallil wrote:If the try is having a return statement and the corresponding catch and finally does not have a return statement then the method must have a default return statement at the end outside these try-catch-finally blocks (if the return type of the method is not void).
So when an excepion occurs before the return statement in the try block,the exception is caught and handled by the catch block, then the code inside the finally block gets executed and at last the default return at the end of the method will get executed and this value is retuned.

See what I have written Ruben, when the finally block is not having a return statement. The default return statement is called.
See the below example

Running the above code gives the following output
Error:4
Reached finally block
Default return
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
You are right, Balagopal. Sorry for the confusion. I failed to read the part where you say that the exception is caught by the catch block. As long as the exception is caught and there is no return in the finally block or the catch block, then the return statement after the try/catch/finally will be executed. If the catch block doesn't catch the exception, then the last return statement won't be executed. But your specifications read otherwise and I didn't read them correctly.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Banu Chowdary wrote:
But why the following is giving Error ???



This is also giving Error. what is the logic in this??? If there is a return only in finally it is giving error. if there is return in only catch it is giving error. But if there is return only in try it is not giving error. Why. Really I am in Confusion. I got this doubt earlier but didn't post it here.



Banu,

The reason why you get compiler errors for both cases is that the last statement (the one after the try/catch/finally statement) is unreachable. In the first case, the compiler can determine that the return inside the catch block will always be executed, and in the second case, the return inside the try block will be executed. This will make the last statement unreachable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unreachable statement