my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions in Overriding methods?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Exceptions in Overriding methods??" Watch "Exceptions in Overriding methods??" New topic
Author

Exceptions in Overriding methods??

Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106


Ans: a) public void m1() throws SQLException,IOException{}
b) public void m1() throws Exception{}
c) None of these

The explaination says that the answer is (C) according to them
public void m1() {}
is the best fit..I agree with that but why can't (a)???

According to overriding rules, the overriding method can declare the same exceptions or it can declare nothing But it should not declare any broader exception.....
Hence, here the answer could be (a) where the overriding method is declaring the same exceptions as the overridden method??
Isn't this true...OR IS there any mystry behind SQLException... In API the io package do not contain this SQL Exception..so where is it???



Regards
Swapnil
[ July 02, 2006: Message edited by: Swapnil Trivedi ]

SCJP 5.0<br />-----------<br />"Help Ever && Hurt Never"
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
SQLException is in java.sql
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The answer I give in this post is wrong! See the post that follows for the reason.

The key to this is the "have to be" in the question
What methods have to be implemented by a class that says it implements I1 and I2 ?


The method m1() "could" declare that it throws SQLException and IOException, or Exception, or no exceptions.

It does not "have to" throw SQLException and IOException, because the programmer may choose to catch all exceptions and declare that it throws no exceptions (by omitting the the "throws" clause).
[ July 02, 2006: 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.
Morten Monrad Pedersen
Ranch Hand

Joined: May 15, 2006
Posts: 31
Originally posted by Barry Gaunt:
The key to this is the "have to be" in the question

The method m1() "could" declare that it throws SQLException and IOException, or Exception, or no exceptions.


I don't agree. Try to look at the following code:


This program won't compile.

The class Tester illustrates the problem with the code. It is implemented correctly with regard to the contract of the interface (I1) that it uses, but if DoubleFace had been allowed, then the invocation of m1() in Tester could throw a SQLException, thus messing up Tester.

The important rule to remember is that one isn't allowed to "broaden" or add to the exception clause of a method when overriding it, and this is exactly what DoubleFace does. DoubleFace states that it can be used in places where I1 could be used, but it adds SQLException to the throws clause of I1.m1(). Similarly DoubleFace states that it can be used in places where I2 could be used, but it adds SQLException to the throws clause of I2.m1().
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Thankyou Morten. I stand corrected.
I found the time to try your example code and convinced myself I was incorrect in my post above.

In fact I do not know why I wrote m1() can throw the broader Exception - I know that rule!

Not my day today.
[ July 02, 2006: Message edited by: Barry Gaunt ]
Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
Thanks Morten and Barry........I got it!!!


Regards
Swapnil
Morten Monrad Pedersen
Ranch Hand

Joined: May 15, 2006
Posts: 31
Originally posted by Barry Gaunt:
In fact I do not know why I wrote m1() can throw the broader Exception - I know that rule!


Well, since you're normally right, we could say that this is the Exception that proves the rule .
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Exceptions in Overriding methods??
 
Similar Threads
Interface method implementation
JQ+ Question ID :957711112180
Another Interface Question..
Broader exceptions thrown by methods that implement interfaces
Override with exception questions