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 Unreachable  statements 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 "Unreachable  statements" Watch "Unreachable  statements" New topic
Author

Unreachable statements

Divya Eknath
Greenhorn

Joined: Jul 27, 2006
Posts: 9
I am confused about the unreachable statements in java.It is said that to place assertions at the place which is unreachable is a good practice, but then again if assert statement is unreachable it gices compile-time error.I referred to JLS to understand the concept of unreachable statements, but sadly i don't have enough time to go through and understand everything as i have exam in few hours
Arvind Sampath
Ranch Hand

Joined: May 11, 2005
Posts: 144
The key here is if the compiler is able to determine that a particular line/segment of code will not be reached during run time, it will flag an Unreachable Code error.



In the above code snippet, the compiler would determine that the last two statements would not be reached. Hence it flags an error.

It is said that to place assertions at the place which is unreachable is a good practice, but then again if assert statement is unreachable it gices compile-time error.


Well the authors of that statement would have most probably meant to put assertions at places which are unreachable logically. Yep, they meant your business logic.

Have a look at this snippet

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Unreachable statements
 
Similar Threads
Exception Doubt
Exam on Monday -- pl help
New SCJP mock exam
What does this compile error mean?
while loop