Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Unreachable Code ?

 
JB Ramesh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
unrachable exception? I get unreachable code error from the compiler.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic