jQuery in Action, 3rd edition
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unreachable code?" Watch "Unreachable code?" New topic

Unreachable code?

Kedar Dravid
Ranch Hand

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

1 public class exception
2 {
3 public static void main(String args[])
4 {
5 System.out.println("A");
6 try
7 {
8 return;
9 }
10 catch(Exception e)
11 {
12 System.out.println("B");
13 }
14 System.out.println("C");
15 }
16 }

1. Compile time error in line no. 8 as main() method is declared void.
2. Program compiles correctly and prints "A" when executed.
3. Program compiles correctly and prints "A" and "C" when executed.
4. Compile time error at line no.14 due to statement not reached.

The answer 1 2.
Now, under what circumstances would line 14 be executed, if at all?
Isn't it unreachable code?
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Yes it is unreachable and will result in a compile-time error.
Look up the errata for the book/article you got it from or if it is not reported, report it.

Tony Morris
Java Q&A (FAQ, Trivia)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Actually under Java 1.5.1_01 it compiles and when run produces the output A.
My attempt at explaining this would be: it is because it is catching Exception it must allow for the possibility that a runtime exception or Error could occur in the try.

Clearly choices 1 and 2 are incompatible, so I guess the 1 is a miscorrected typo.
[ January 23, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
My apologies.
It is unreachable code and will NOT result in a compile-time error.
I am too used to my code style checkers telling me about it at build time.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
It has been well established on this board that an empty try block plus a catch clause with Exception does not make its catch block unreachable. If there is a possibility that an empty try block can throw an Exception, there is a possibility an Exception will occur before the return statement in line 8 is executed. In that event, line 14 is reached.

Mike Gershman
SCJP 1.4, SCWCD in process
shri mon
Ranch Hand

Joined: Jan 11, 2005
Posts: 63
public class Test
public static void main(String[] args)

This compiles and runs without errors.
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Well, sorry for the typo!
The answer I get is option no. 2.
Well, if some code is unreachable (as per Tony), why isn't option no. 4 valid?
The statement System.out.println("C"); is outside the catch block.
Nikhil Bansal
Ranch Hand

Joined: Jan 24, 2005
Posts: 60
Hi All

As said by the members, the code compiles and prints the o/p as A. I believe tht why C does not get printed is coz of the return statement in try block. Due to return statement the control does not reach C.



Swapan Mazumdar
Ranch Hand

Joined: Jul 23, 2003
Posts: 83

The compiler complains about "System.out.println("C") //unreachabe statement
" in this case.
I wonder why it thinks "the return statement" in the try block doesn't make that statement unreachable. Isn't it unfair?

[ January 24, 2005: Message edited by: Swapan Mazumdar ]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
it's quite OK to have a return statement in a try block.

In fact it gives "unreachable code" on System.out.println("C");, not on the second return;
As there's no path through which this can be reached that's the be expected.

That's on 1.4 and 1.5 compilers both btw.

Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Let me rephrase my question a bit,
The code at the top of the page compiles without error.
I was expecting a compile-time error because the statement 'System.out.println("C");' is unlikely to be executed in any case.
So, why is the 4th option not the correct answer?
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

I think Mike Gershman's answer answered that question pretty well.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Richard Vagner
Ranch Hand

Joined: Jun 26, 2001
Posts: 108


Under what circumstances, an empty try block
would throw an Exception?
[ January 24, 2005: Message edited by: Richard Vagner ]
I agree. Here's the link: http://aspose.com/file-tools
subject: Unreachable code?
It's not a secret anymore!