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

Exceptions

 
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hello All,
Why should the subclass methods not throw a broader exception than the overridden super class method?

Kindly explain with examples.


Thanks,
Prasanna
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Prasanna Raman wrote:Hello All,
Why should the subclass methods not throw a broader exception than the overridden super class method?

Kindly explain with examples.


Thanks,
Prasanna



If we allowed to do that, then how do you resolve the polymorphic invocation of methods?
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Could you please give me an example?

I know the answer is that it would affect run-time polymorphism but I am not able to quite comprehend how that would work.

Thanks,
Prasanna
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Since you cross-posted here (thank you, DB, for noticing), people will assume you have cross-posted again, and will be reluctant to answer.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Check out the Liskov substitution principle.

In this case, in short, a calling class expects only a few exception types. If then another exception is thrown it doesn't know how to handle it.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This question, while superficially different, is basically the same question you asked about method overriding, and the answers are the same.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

David Newton wrote:This question, while superficially different, is basically the same question you asked about method overriding, and the answers are the same.

In which case, please read this FAQ, while I "pull rank" and close this thread.
 
    Bookmark Topic Watch Topic
  • New Topic