• 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

Override with exception questions

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from enthuware

Consider these two interfaces:

interface I1
{
void m1() throws IOException;
}
interface I2
{
void m1() throws SQLException;
}

What methods have to be implemented by a class that says it implements I1 and I2 ?

A Both, public void m1() throws SQLException; and public void m1() throws IOException;
B public void m1() throws Exception
C The class cannot implement both the interfaces simultaneously as they have conflicting methods.
D public void m1() throws SQLException, IOException;
E None of the above.

The answer is E.

I understand it's explaination on this question. But it did not explain why D is not right. Can someone explain to me why D is not right?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is that if you throw SQLException you are throwing a checked exception which is not declared by I1.m1() method, and if you throw IOException you are throwing a checked exception which is not declared by I2.m1(). When you implement I1 and I2 your m1() method must properly override both I1.m1() and I2.m1(). For that, your method must not throw any checked exceptions in this case (since there is no checked exception which is a subclass of both SQLException and IOException.)
 
Henry Zhi Lin
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:The reason is that if you throw SQLException you are throwing a checked exception which is not declared by I1.m1() method, and if you throw IOException you are throwing a checked exception which is not declared by I2.m1(). When you implement I1 and I2 your m1() method must properly override both I1.m1() and I2.m1(). For that, your method must not throw any checked exceptions in this case (since there is no checked exception which is a subclass of both SQLException and IOException.)



Ruben, thanks for your brief explaination.

Problem solved.

Cheers.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to know, Henry.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

do we have to know the exception inheritence tree for the exam? I mean, the exam makers could be so evil to use instead of the SQLException the EOFException, which COULD result in an other answer. Ok, that EOFException is an subclass of IOException is obvious, but that's not always like that.

cheers
Bob
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Wheeler wrote: I mean, the exam makers could be so evil to use instead of the SQLException the EOFException, which COULD result in an other answer.



No the exam makers are not that evil. Although the example that you gave is pretty obvious and you should know about it, but you can stick to the list of exceptions given in the K&B book, that should be good for the exam...
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:

Bob Wheeler wrote: I mean, the exam makers could be so evil to use instead of the SQLException the EOFException, which COULD result in an other answer.



No the exam makers are not that evil. Although the example that you gave is pretty obvious and you should know about it, but you can stick to the list of exceptions given in the K&B book, that should be good for the exam...



If they stick to that list, then it's fine.

Thanks Ankit.
cheers
Bob
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic