• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

(un)reachable code?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I've come upon a question in the Whizlabs SCJP simulator and I need some clarifications. Look at the following code snippet:


Why doesn't the compiler reject this code? The code inside the catch block seems unreachable to me... I mean there's no exception thrown so how could the System.out.println statement be reached? Note that I've replaced Throwable by some other exception type [now checked] - IOException and now the compiler correctly points out the problem. Is the exception type inside the catch block that makes the compiler accept this code?

Thanks for any input.
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It may be it is something related to checked and unchecked exception .
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
What you are throwing the the superclass of all exception, i,e it includes both checked exceptions and unchecked exceptions. So the compiler doesn't complain about it. When you are trying to throw a IOException it is a checked exception, it looks for i/o code, once it finds it unavailable at compile time, it starts complaining. You could try with RunTime exception, instead of Throwable it may not complain( I haven't tried it, when i wrote this, let me try and come back to you ).

Regards
Shivakanth
reply
    Bookmark Topic Watch Topic
  • New Topic