File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
    Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
Prasanna Raman
Ranch Hand
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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: 375
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20398
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
    Bookmark Topic Watch Topic
  • New Topic