yesterday i came along some issues with unreachable code.
I am training with enthuware.
Here the questions:
What will the following code print, when run?
Explanation: Try to follow the flow of control :
1. in method(1) : i is not 2 so, j gets "1" then finally is executed which makes j = "13" and then the last statement (j +=4) is executed which makes j = "134".
2. in method(2) : i is 2, so it goes in the if block which throws an exception. So none of the statements of try block are executed and control goes to catch which makes j = "1342", then finally makes j = "13423" and the conrol is returned. Note that, the last statement ( j+=4) is not executed as there was an exception thrown in the try block.
Now the second question:
Following is a supposedly robust method to parse an input for a float.... (See Exhibit) Which of the following statements about the above method are true??
If input: "0.1" then it will return 0.1 and print finally.
If input: "0x.1" then it will return Float.Nan and print Invalid Input 0x.1and finally.
If input: "1" then it will return 1.0 and print finally.
If input: "0x1" then it will return 0.0 and print Invalid Input 0x1 and finally.
The code will not compile.
Explanation: Note that the return statement after finally block is unreachable. Otherwise, if this line were not there, choices 1, 2, 3 are valid.
The main difference between the two given codes is, that the second one has a return statement in the try block and therefore that last return is unreachable.
To understand these two codes you should know one thing, finally always executes. Even if there are return statements involved. The first time you invoke method(1) the flow of your code never encounters a return statement and executes 'till the end'. On the other hand, when you invoke your parseFloat method, you will encounter a return statement, with or without an error, within your try/catch/finally statements.That makes your last return unreachable in parseFloat;
Does all this make sense? Maybe some one will come with a better way of explaining
Now look here
in the first case
we have not written the return statement in the try block
This compiles very fine let us see now why
first of all we will see what does it do if we do not write return statement in the try block
1. When we do not write return statement in the try block, the control is not going to get returned
through the try block
2. This means that the corresponding catch block or the finally block will execute and if the exception is not thrown then the catch block does not get executed and after finally block has run , as the exception is not thrown, the code following the finally block will get executed
3. so when you are considering that line j+="4" should be unreachable, it is not true
it wil be unreachable if we write the return statement in the try block
what do we do here?
we wrote a return statement
now what does that mean?
even if the exception is thrown in the try block or not
return to the calling method BUT AFTER executing the finally block
here as we wrote the return statement in try block
the compiler knows for sure that in either case (exception is thrown or not thrown)
the control is going to the calling method after TRY and FINALLY block has executed and hence the code below the finally block is not getting reached
hence the compiler shows the error that
F:\Java\Javaranch problems>javac Test1.java
Test1.java:26: unreachable statement
j += "4"; // should be unrechable
hope this helps and makes better understanding
Sebastian I know you are going to get certified this time , you are the winner and you have powers from within
best luck friend
SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Joined: Mar 19, 2009
Hi Martin, Prasad,
thanks for helping me. I think i do now understand. It's all about the return statement and it's positioning.
Since we have no exception, we wont have a return. Therefor the compoundstatement will be reachable.
Since we have a return in try block and catch block, the compoundstatement will be unreachable.
Thanks for your trust in me. I will do the exam within 2 1/2 weeks. When i pass the exam, you will definately hear from me :-)
I think you must be clear about Exception Handling concepts, the only problem you are facing is the flow.
There are only two possibilities.
Possibility 1:- Exception is thrown .
Possibility 2 :- Exception is not thrown.
Possibility 1:- <Without return statement in catch> When exception is thrown the flow is (First eligible Catch Block) ,then (Finally) and then (Rest of the method after try-catch)
<With return statement in catch> When exception is thrown the flow is (First eligible Catch Block) ,then (Finally) and then (calling method because return is encountered) . In this case rest of the method is not get executed.
Possibilty 2 :- <Without return statement in try> In this case just whole try block got executed followed by finally and then rest of the method statements.
<With return statement in try> In this case just whole try block got executed followed by finally and then back to calling method, In this case rest of the method is not get executed.
In a nutshell,
As you know that whenever you encounter return statement , you have to move back to the calling method. In case of try-catch , just remember to execute statements in finally before returning.
In your first code, the statement is perfectly reachable in the case when "NO exception " is being thrown.
In your second code, the statement is not reachable in both the cases ie when exception is thrown and when exception is not thrown hence "Unreachable error" .
SCJP 6.0 96%
(Connecting the Dots ....)
Joined: Mar 19, 2009
Thank you for your explanation. I thin i've got it now