• Post Reply Bookmark Topic Watch Topic
  • New Topic

throw checked Exception in if-else clause  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


here expression in if condition is a compile time constant expression so compiler knows that definitely if block is going to be executed and in if block there
is a checked Exception being thrown thus the code below if block is never reachable.But why is it not giving compile time error that line5 is unreachable.???



thanks in advance
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question. "If" is special in respect to "if true". If you were to write "while true", the compiler would pick up on te fact that the code was unreachable. "If true" is designed to alllow easy changing from true to false because people write code like


Java wants to make it easy for you to turn off debugging by turning off that compile time constant so they still let the code compile in the case you listed in your post.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!