• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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: 23928
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
 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic