wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Java Unreachable Code ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Unreachable Code ?" Watch "Java Unreachable Code ?" New topic
Author

Java Unreachable Code ?

JB Ramesh
Greenhorn

Joined: Jan 09, 2007
Posts: 20
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

Joined: Jan 10, 2002
Posts: 60748
    
  65

"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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
unrachable exception? I get unreachable code error from the compiler.
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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 ]

~Sanjeev Singh<br />SCJP 1.5
Ajay Singh
Ranch Hand

Joined: Dec 13, 2006
Posts: 182
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

Joined: Nov 01, 2006
Posts: 381
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

Joined: Jan 09, 2007
Posts: 20
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Unreachable Code ?
 
Similar Threads
dan exam doubt 14
Dan's Exception Handling question
How to find unreachable stmt?
Unreachable statement
Phantom Reference