| Author |
Overriding with Exceptions
|
anand phulwani
Ranch Hand
Joined: Sep 10, 2005
Posts: 242
|
|
We know when we are overriding than we can specify a Subset of the exception of the base class in the derived class method So this compiles fine and this gives the error as it should this give the compiler error Test.java:8: methoda(int) in Test cannot override methoda(int) in a; overridden method does not throw java.lang.Exception void methoda(int a) throws Exception ^ 1 error Now the Question Comes Why this compiles fine,we know that ClassCastException is a subclass of Exception,also the same happens when i try this with AssertionError and Error I will be grateful if someone clears the confusion
|
Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%
|
 |
Sandeep Chhabra
Ranch Hand
Joined: Aug 28, 2005
Posts: 340
|
|
Hello Mr. Anand, I think the ans to your question is that while overriding we can specify a Subset of the exception of the base class in the derived class method but this thing is true only for Checked Exceptions. RuntimeExceptions and Errors are not checked exceptions and the compiler does not checks whether the overriding method is throwing proper subclasses of RuntimeExceptions and Errors. Please Correct me if I am wrong. Sandy
|
Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
|
 |
agrah upadhyay
Ranch Hand
Joined: Sep 01, 2005
Posts: 579
|
|
Mr. Sandeep , U Are Olright.Sinse Error And RuntimeException Are UnChecked EXception,they Need Not 2 B Included In throws Clause.Also They Can b Included In Throws Clause ,InDependent Of Other Checked Xception. #### Agrah Upadhyay 3rd Year B.Tech
|
<i>--Agrah Upadhyay--</i><br />Final Year B.Tech SCJP,SCWCD,SCBCD <br /> <br /><b>Now since the real test for any choice is having to make the same choice again,knowing full well what it might cost.</b>-Oracle
|
 |
anand phulwani
Ranch Hand
Joined: Sep 10, 2005
Posts: 242
|
|
dear mr sandeep, i would appreciate if you can explain what are the cases in which this thing will work for e.q. two we already know (Case1) Base class throwing any Exception or no Exception The Derived Class throwing Error (Case2) Base class throwing any Exception or no Exception The Derived Class throwing RuntimeException Please Confirm me by posting whether there are any other cases, or this is all.
|
 |
Arvind Giri
Ranch Hand
Joined: Jun 26, 2005
Posts: 91
|
|
Hi Anand, I think following will work : Base class checked Exceptions + unchecked Exception sub class subclasses of checked Exceptions declared in Base class + any of unchecked Exceptions(not necessary to be from base class) please correct me if I am wrong.
|
Regards<br /> <br />Arvind Giri<br />MCA,SCJP 1.4,SCWCD 1.4<br />Looking for SCDJWS
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
Following Sandeep's post: To see what is going on, just remove all those Errors and unchecked exceptions (RuntimeException and subclasses thereof) in the above code. They do not take part in any of the decisions as to whether or not overriding is allowed. You will then be left only with Exception. Try some examples with other checked exceptions, for example: java.io.FileNotFoundException, java.io.IOException, java.lang.InterruptedException. [ September 17, 2005: Message edited by: Barry Gaunt ] [ September 17, 2005: Message edited by: Barry Gaunt ]
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
 |
|
|
subject: Overriding with Exceptions
|
|
|