This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unreachable code ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unreachable code ?" Watch "Unreachable code ?" New topic
Author

Unreachable code ?

Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Consider the following code:

public class exception {
public static void main(String args[]) {
System.out.println("A");
try {
return;
}
catch(Exception e) {
System.out.println("B");
return;
}
System.out.println("C");
}
}

When I tried to compile this, I got:
exception.java:11: unreachable statement
System.out.println("C");
^
1 error


Now, consider the following code:
public class exception1{
public static void main(String args[]){
System.out.println("A");
try {
return;
}
catch(Exception e) {
System.out.println("B");
}
System.out.println("C");
}
}

O/p: A

How is it that the first program results in a compile-time error, while the second one compiles and runs without any errors? The only difference between the 2 programs is that the first program has a return statement in the catch block, which I guess, is inconsequential. In both cases, the statement System.out.println("C") is outside the try-catch block.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
in fact the return statement in the catchblock is crucial.
Think the logic through and you'll find out why: the return statement in the catch block combined with the return statement in the try block means that any code after the catch block (which after all catches everything) can no longer be reached.


42
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
In both cases, there is a return statement in the try block. So, I guess the catch block itself is never executed. Then, how does the catch block come into the picture? Plz guide.
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Kedar

Yes in your program probably the catch block would never get executed but still the compiler doesn't know that this block of code would not give any error.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Kedar, there are buttons underneath the Add Reply Button. One o those buttons says "CODE". You can use this button to post code into your post and the code will keep its indentations, which makes the code more readible.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
 
subject: Unreachable code ?
 
Similar Threads
Finally abrupt return
Unreachable code?
ghost - unreachable statement
Un reachable statement (Sorry for posting the same topic second time...)
PLease Explain Below Output