This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

code not reachable error

 
vipul patel
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
G'Day Guys,

Can any body point out situations where compiler yells "Code not reachable"?
 
Raghusham Sankargal
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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
 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • 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
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLS - unreachable statements
 
vipul patel
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys
 
Raghusham Sankargal
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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).
 
Pratik Lohia
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few would be

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

OR


if(a==b)
{
return a;
i=0;
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic