File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

code not reachable error

 
vipul patel
Ranch Hand
Posts: 146
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLS - unreachable statements
 
vipul patel
Ranch Hand
Posts: 146
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys
 
Raghusham Sankargal
Ranch Hand
Posts: 59
  • 0
  • 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
  • 0
  • 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;
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic