Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

No Unreachable error ?

 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code, statement 'System.out.println("i="+i);' will never be reached. This could be identified during compilation by compiler and it should give 'Unreachable error"
Why this is not happening?

 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The javac compiler does not evaluate expressions looking for exceptions. For example, even if the expression were simply 5/0, the compiler will not assume that an exception will occur.

In addition, the Java compiler only looks at method headers, not method imlementations or javadoc. So javac will not know that Math.random() is always less than 1 and (int)Math.random() is always zero.
[ December 06, 2004: Message edited by: Mike Gershman ]
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mike.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic