Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding with Exceptions

 
anand phulwani
Ranch Hand
Posts: 242
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
agrah upadhyay
Ranch Hand
Posts: 579
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
anand phulwani
Ranch Hand
Posts: 242
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic