• 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

Java Unreachable Code ?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Following block compiles..

if(false){
System.out.println("Done");
}

But following does not compile... Says un reachable exception....

while(false){
System.out.println("Done");
}


Anyone knows why compiler is dealing differently though both System.out statements are not reachable.

Thanks in advance.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Ramesh JB",

Welcome to the Ranch. There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

I believe the first situation is a special case, handled differently by the compiler so you can conditionally compile code based on a static boolean member. i.e. you can use this first form to turn a block of code on or off based on a boolean constant. The same special case doesn't apply to the "while" block.
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
unrachable exception? I get unreachable code error from the compiler.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajay,
Please revisit the things that Ernest has said.None of the statements in the conditional block(if) and loop(while) will be reached ever,but for if it is a exceptional case,so compiler will not complain with the if(true)'s block.
[ January 09, 2007: Message edited by: Sanjeev Kumar Singh ]
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sanjeev,


for if it is a exception case,so compiler will not complain with the if block.


asker didn't clarify that, I am trying to get this clarification from the asker whether my understanding is correct. I never said
Ernest was incorrect.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajay I think you are right means you understood the right things.Any way there is not as such exception case as I wrote,I modified my last post for the same.
 
JB Ramesh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi People,

Thanks for the responses. I mentioned 'Unreachable exception' in my posting. Actually I meant 'Unreachable code' compile time error. Sorry.

Thanks Ernest for clearing the doubt.
 
A feeble attempt to tell you about our stuff that makes us money
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic