• 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

throws problem

 
Ranch Hand
Posts: 47
Redhat Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
below two program is given -
why first program compile but second not ???





 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that, in order to catch a Checked exception you are required to invoke code that (might) throw said exception. java.lang.Exception however is potentially a RuntimeException, for which this rule does not apply.

EDIT: It's interesting to note though, that a method is obviously allowed to throw a Checked Exception whether or not code that might throw it has been invoked.

// Andreas
 
Saloon Keeper
Posts: 14079
319
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And with good reason too! What if we have an extensible class, that has a default implementation of some method, which doesn't throw any exceptions; but it can foresee that any subclasses may want to throw that particular exception when they override the method? If the base class doesn't declare the method to be capable of throwing that exception, then the subclasses can't.

Take a look at the finalize() method, for example. Object's implementation does absolutely nothing, but it still declares the method to throw a Throwable, so subclasses that override finalize() may throw any exception they want.
reply
    Bookmark Topic Watch Topic
  • New Topic