• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

COnfusion About Unreachable Code

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
     
    Ranch Hand
    Posts: 241
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    Ranch Hand
    Posts: 446
    1
    Eclipse IDE MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    sebastian tortschanoff
    Ranch Hand
    Posts: 68
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    Ranch Hand
    Posts: 316
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 !!!

     
    sebastian tortschanoff
    Ranch Hand
    Posts: 68
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for your explanation. I thin i've got it now
     
    Ranch Hand
    Posts: 30
    Eclipse IDE
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    good explanation
    reply
      Bookmark Topic Watch Topic
    • New Topic