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 COnfusion About Unreachable Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "COnfusion About Unreachable Code" Watch "COnfusion About Unreachable Code" New topic
Author

COnfusion About Unreachable Code

sebastian tortschanoff
Ranch Hand

Joined: Mar 19, 2009
Posts: 68
Hello Salooners,

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?



Answers:
  • 13432
  • 13423
  • 14324
  • 12434
  • 12342


  • 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??



    Answers:

  • 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.

    _____________________________________________________


    Now i am confused. IMHO the first code-snipped should not compile, like the second example. But how can answer 13432 be correct?

    Could anyone explain


    Thanks in advance,

    sincerely yours


    Power from within.

    Failed SCJP 2 times :-(
    Martin Vanyavchich
    Ranch Hand

    Joined: Sep 16, 2008
    Posts: 241
    Ok, let me take a shot at explaining this one

    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

    Regards,

    Martin


    SCJP 6, OCMJD 6, OCPJWSD 6
    I no good English.
    Prasad Kharkar
    Ranch Hand

    Joined: Mar 07, 2010
    Posts: 438

    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
    like this

    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
    ^
    1 error


    hope this helps and makes better understanding
    happy preparation
    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%] June 30th, 2010
    OCPWCD [84%] March 26th, 2013
    If you find any post useful, click the "plus one" sign on the right
    sebastian tortschanoff
    Ranch Hand

    Joined: Mar 19, 2009
    Posts: 68
    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 :-)


    sincerely yours,
    Sebastian
    Sahil Kapoor
    Ranch Hand

    Joined: Sep 12, 2009
    Posts: 316
    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)


    OR



    <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.

    OR

    <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" .

    Thanks !!!



    SCJP 6.0 96%

    (Connecting the Dots ....)
    sebastian tortschanoff
    Ranch Hand

    Joined: Mar 19, 2009
    Posts: 68
    Thank you for your explanation. I thin i've got it now
    Sandy Ghosh
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 30

    good explanation
     
     
    subject: COnfusion About Unreachable Code
     
    Similar Threads
    JQPlus Question
    return
    JQ+ and confusion with try/catch/finally
    why is finally not being called?
    Unreachable Code in try/catch ? why????