This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how the compiler decides about the unreachable stmt 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 "how the compiler decides about the unreachable stmt" Watch "how the compiler decides about the unreachable stmt" New topic
Author

how the compiler decides about the unreachable stmt

NareshAnkuskani Kumar
Ranch Hand

Joined: Sep 15, 2005
Posts: 76
--------------------------------------------------------------------------------
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.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
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
    Murali Mohan
    Ranch Hand

    Joined: Jan 09, 2006
    Posts: 66
    Thank you Edwin Dalorzo.
    Great explanation...


    Thanks,<br />Murali...
    shandilya popuru
    Ranch Hand

    Joined: Dec 21, 2004
    Posts: 95

    hats off to your explanation Edwin Dalorzo


    sandy
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: how the compiler decides about the unreachable stmt
     
    Similar Threads
    difficult to understand the java compiler?
    No Unreachable error ?
    unreachable code in 'finally'
    Unreachable, return in finally or catch
    finally block executes right after the return statement?