Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unreachable code?

 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
shri mon
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test
{
public static void main(String[] args)
{
return;
}
}

This compiles and runs without errors.
 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Regards

Nikhil
 
Swapan Mazumdar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Mike Gershman's answer answered that question pretty well.

Mark
 
Richard Vagner
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike,

Under what circumstances, an empty try block
would throw an Exception?
[ January 24, 2005: Message edited by: Richard Vagner ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic