• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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
 
Sheriff
Posts: 27451
88
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
Sheriff
Posts: 27451
88
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..
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic