• 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

How to find unreachable statements

 
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
The following code give compiler error...
unreachable statement System.out.println("Bye");



are there any standard rules for finding these errors other than tracing the logic....

Thanks in advance...
 
Ranch Hand
Posts: 231
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most IDEs should remind you that some lines may have "unreachable code", if you pasted the above into IntelliJ you'd see a red line.

Depends what you use, if you use notepad/VI then you wont get any help. Other than that you'll have to look through at your scoping
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler is a computer program. So if a compiler can do something, then clearly there must be standard rules for how to do that thing. So the answer to your question is "Yes".
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Paul ,James Elsey
Actually i encountered similar questions while solving some mock tests...
So i need to determine without compiling or using an IDE..

Can any one provide references or suggested readings regarding unreachable statements...
 
Greenhorn
Posts: 12
Google Web Toolkit Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to run the logic. If an exception is guaranteed to be thrown then everything after it is not going to run. The compiler sees that.

For example:


The compiler sees that this exception is going to always be thrown. In the case of the question above, the finally bloc is supposed to always run (if there is not a System.exit(1) in the other blocs). To the compiler it sees that an exception is always thrown just like in my example I just gave.
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Eric ,
Now should i conclude that if compiler encounters some code which is certainly not run
then it will show an error ??

One more case is code below infinite loops (without any break statements)...
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends what you mean by "certainly". The compiler can't identify all possible infinite loops (in fact there's a mathematical theorem called the "Halting Problem" which proves that it's impossible to write a program to identify whether a program will terminate or not). But it does identify some of the more obvious infinite loops.

But I don't understand your goal. Do you want to be able to look at a program and figure out whether a loop will terminate? Or do you want to figure out whether the compiler will figure that out?
 
Eric Kizaki
Greenhorn
Posts: 12
Google Web Toolkit Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think part of it depends on what we call "compile time constants." This code is illustrative:


A human would see that b is true and would cause an infinite loop. Compiler is not able to evaluate the variable into a compile time constant.

The cool thing about Java: create your own programs and play around with them!
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Paul for your reply...

Actually my motto is to answer the questions which include these unreachable statements..
 
Look ma! I'm selling my stuff!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic