Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Unreachable code?

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Mike Gershman's answer answered that question pretty well.

Mark
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic