Author
Exceptions
Prasanna Raman
Ranch Hand
Joined: Sep 05, 2010
Posts: 48
posted Sep 06, 2010 23:50:29
0
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
posted Sep 06, 2010 23:58:16
0
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: 48
posted Sep 07, 2010 00:00:48
0
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: 32599
posted Sep 07, 2010 02:39:39
0
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: 19214
posted Sep 07, 2010 03:03:57
0
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
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12617
posted Sep 07, 2010 03:23:47
0
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: 32599
posted Sep 07, 2010 06:04:10
0
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: jrebel
subject: Exceptions