• 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

code not reachable error

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
G'Day Guys,

Can any body point out situations where compiler yells "Code not reachable"?
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"code not reachable" - This error comes when the code what you have written may not be executed at all.

For Example: while writing catch blocks, in the parameter field child exception elements should come first before their parents. That is


This gives you an unreachable code exception because the Exception catch block can catch even NumberFormatExceptions also.

The reason why Exception catch block can catch NumberFormat Exceptions: We know that NumberFormatException is a child of Exception class. So the parent refernce can be used for child objects also right.

The above code is one way where you can get the Unreachable code error. Hope you can find others on your own, if the above concept is explained
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sham ,I dont think so ,

the one which u have explained I think gives Exception already caught.

correct me if I am wrong
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS - unreachable statements
 
vipul patel
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys
 
Raghusham Sankargal
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello harish,
I use Eclipse 3.0 version. This is the message I get when I ran that has the code forgment posted in previous message - "Unreachable catch block for NumberFormatException. It is already handled by the catch block for Exception". I am quite sure it doesn't make any difference whether you are using IDE or not, or may be which IDE you are using. Hope I corrected you if I am right. I think we both are right. but as that is an unreachable code, it gives us an explaination why it is unreachable (The answer given by you).
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few would be

for(...)
{
if(a==b)
{
break;
return a;
}
}

OR


if(a==b)
{
return a;
i=0;
}
 
Beauty is in the eye of the tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic