• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exceptions in Overriding methods??

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SQLException is in java.sql
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Morten and Barry........I got it!!!


Regards
Swapnil
 
Morten Monrad Pedersen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic