Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

how the compiler decides about the unreachable stmt

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
--------------------------------------------------------------------------------
hi guys below i am listing the code plz refer it once

class one
{
public static void main(String a[])
{
System.out.println("first stmt");
return;
System.out.println("second stmt");
}
}
i am very much clear that the above code results in compile time error that
the stmt after return stmt is unreachable.

look at the other code below

class two
{ public static void main(......)
{
try
{
return;
}
catch(.....)
{
}
finally
{
System.out.println("due to finaly");
}
System.out.println("out of try-catch-finally stmt");//3
}
}

in the above code (i.e) the second code why i am not facing the same problem which i came across during the compilation of first code.
because i am very sure that the statement marked '//3' is surely not reachable.
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SHORT ANSWER
If an exception happens in the try block, and you handle it, and manage to pass to the next statement out of the try-catch-finally block, then that statement is actually reachable.



Not the case of your first example, pal.

Regards,
Edwin Dalorzo

LONG ANSWER

I have an interpretation of this behavior based on the explanations of the JLS chapter 14 section 21.

The JLS determines if a statement is reachable based on two principles

  • Whether the statement is reachable.
  • Whether the statement can complete normally.


  • Now, the JLS states that a break, continue, return, or throw statement cannot complete normally.

    Now, this sounds kind of obvious for some statements.

    Interpretation #1
    That means any statement after any of this statements could be considered unreachable.

    Evidence #1




    Interpretation #2
    Then, JSL suggests that a finally block is reachable if its try statement is reachable. So in this code the finally statement is reachable, no matter that the try block finish abnormally.

    Evidence #2


    However, the JLS also states that �[�] Every other statement S in a nonempty block that is not a switch block is reachable if and only if the preceding statement S can complete normally�

    Interpretation #3
    As the try block in previous statement did not complete normally, the next statement after the try-finally block is unreachable, as this evidence shows.

    Evidence #3


    Finally, the JLS states that a Catch block is reachable "[...] if some expression or a throw statement in the try block is reachable and can throw an exception whose type is assignable to the parameter of the catch clause [...]"

    Interpretation #4
    This statement seems to be override the previous one, because althought the return stament always finish abnormally it might be subject to cause an exception in a try-catch-block, and hence the next evidence is valid, because the cath block is subjet to finish normally if you handle the exception, and if so, the statement after try-catch-finally may be executed. Let's see some evidence

    Evidence #4


    I hope this helps.

    Regards,
    Edwin Dalorzo
     
    Ranch Hand
    Posts: 66
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you Edwin Dalorzo.
    Great explanation...
     
    Ranch Hand
    Posts: 95
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    hats off to your explanation Edwin Dalorzo
     
    It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
    free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic