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
posted
0
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.
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 ]
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
posted
0
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
posted
0
public class Test { public static void main(String[] args) { return; } }
This compiles and runs without errors.
Kedar Dravid
Ranch Hand
Joined: May 28, 2004
Posts: 333
posted
0
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
posted
0
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
ban$al
Swapan Mazumdar
Ranch Hand
Joined: Jul 23, 2003
Posts: 83
posted
0
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
posted
0
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.
42
Kedar Dravid
Ranch Hand
Joined: May 28, 2004
Posts: 333
posted
0
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?