aspose file tools*
The moose likes Java in General and the fly likes Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Exceptions

Prasanna Raman
Ranch Hand

Joined: Sep 05, 2010
Posts: 335
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

Joined: Nov 04, 2009
Posts: 2066

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?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Prasanna Raman
Ranch Hand

Joined: Sep 05, 2010
Posts: 335
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

Joined: Oct 13, 2005
Posts: 39773
    
  28
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

Joined: Oct 27, 2005
Posts: 19757
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

This question, while superficially different, is basically the same question you asked about method overriding, and the answers are the same.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
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
 
subject: Exceptions