• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Understanding Exception Hierarchy

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt about exceptions

It seems the parent class method throws any child or sibling of RuntimeException, and the child throws RuntimeException this is ok. THe compiler accepts it and even runs the program.
However when parent class method throws any child or sibling of RuntimeException, and the child throws Exception this is not ok

In the above code change RuntimeException to Exception ...
I'm having a hard time understanding this, can anyone explain this rule?
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It seems the parent class method throws any child or sibling of RuntimeException, and the child throws RuntimeException this is ok.


RuntimeException is unchecked. Your child can throw any unchecked exception irrespective of whether the parent throws any exception.

when parent class method throws any child or sibling of RuntimeException, and the child throws Exception this is not ok


Exception is checked. If the child throws a checked exception then this has to be the same or a subclass of the exception thrown by the parent.
John
 
Ranch Hand
Posts: 64
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case of overriding child class method can not throws wider exception than parent class method in case of checked exception,
but in case of Runtime exception there i no such restriction,
you are throwing Exception in child class and this dose not come under Runtime
so it will not work
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some time it also good to catch the exception and show some other suitable exception from method, called exception chaining though you need to include original exception as source for more information on stack trace.
 
A Phatak
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the succint explanation...Found the same in Kathy Sierra book in the grey rectangle ... Wonder how I missed

John Stark wrote:

It seems the parent class method throws any child or sibling of RuntimeException, and the child throws RuntimeException this is ok.


RuntimeException is unchecked. Your child can throw any unchecked exception irrespective of whether the parent throws any exception.

when parent class method throws any child or sibling of RuntimeException, and the child throws Exception this is not ok


Exception is checked. If the child throws a checked exception then this has to be the same or a subclass of the exception thrown by the parent.
John

 
reply
    Bookmark Topic Watch Topic
  • New Topic