This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes code not reachable error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "code not reachable error" Watch "code not reachable error" New topic
Author

code not reachable error

vipul patel
Ranch Hand

Joined: Oct 16, 2005
Posts: 146
G'Day Guys,

Can any body point out situations where compiler yells "Code not reachable"?
Raghusham Sankargal
Ranch Hand

Joined: Oct 10, 2005
Posts: 59
"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


Raghusham Sankargal
harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
Sham ,I dont think so ,

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

correct me if I am wrong


Harish<br />SCJP 1.4 (85%)
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
JLS - unreachable statements
vipul patel
Ranch Hand

Joined: Oct 16, 2005
Posts: 146
Thanks guys
Raghusham Sankargal
Ranch Hand

Joined: Oct 10, 2005
Posts: 59
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

Joined: May 05, 2005
Posts: 88
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.
 
subject: code not reachable error
 
Similar Threads
reachable statement
JDBC Connection Problem when I put my code in a package
if-else confusion
Code not reachable
Unreachable statement