• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unreachable statement

 
guillaume marchand
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BUt if there is a System.exit(); in try or catch block then finally block will not be executed.
 
Hs Arun
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok ! Thanks for those explanations.
 
Lourenço Marinho
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic