• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

throw checked Exception in if-else clause

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic