• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unreachable Code

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 }

Source: http://www.go4java.20m.com/mock1.htm

According to me it should throw unreachable code error during compilation
but the output is A.
Can anyone explain this?
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

what is the difference between
- Exception
and
- IOException
?

Good luck.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

that behavior is due to the try-catch block. If exception occurs in the try block then control is moved to the catch block but the catch block does not do much just print out something after the printing statement the program continue execution normally. If you rethrow the exception in the catch block then the last print statement wont be reached and THEN it is a compiler error.

Hope that helps.

cheers.
 
Tina Tibrewal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IOException is a subclass of Exception.
 
Tina Tibrewal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even in this the last print statement is not reached then
why doesnt it give an error?
 
Omer Haderi
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compiler just does not know what might happened in the try block... There might not be an error (so the method will return), But there may be an exception AND therefor the execution will continue until the end. SO that is not a compile time error.

cheers,
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tina Tibrewal:
IOException is a subclass of Exception.



Correct.
Which one is checked?

Good luck.
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dang,

I hate this code snippets that are not formatted.
The catch has a body with a System.out.

Omer is of course correct.

Sorry.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tommaso Nuccio:
Dang,

I hate this code snippets that are not formatted.
The catch has a body with a System.out.

Omer is of course correct.

Sorry.



Actually, *both* Tommaso and Omer is correct. The reason "C" is reachable is because there is a possibility of an exception occuring. But the reason that an exception is allowed is because Exception includes unchecked exceptions. If the catch was simply an IOException, then the catch block would have been considered unreachable code.

Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic