Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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 .
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic