• 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 and interfaces

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my code I am experimenting with based on enthuware's test question:
import java.io.*;
import java.sql.*;
interface I1
{
void m1() throws IOException;
void m2() throws IOException;
}
interface I2
{
void m1() throws SQLException;
void m2() throws FileNotFoundException;
}
public class TestClass implements I1, I2{
public static void main(String args[]) //throws Exception
{

TestClass tc = new TestClass();
I1 i1 = (I1) tc; //This is valid.
try {
i1.m1();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
i1.m2();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

I2 i2 = (I2) tc; //This is valid too.
// i2.m1();
try {
i2.m2();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}
public void m1() throws IOException, SQLException
{
System.out.println("Hi there");
}
public void m2() throws FileNotFoundException
{
System.out.println("How are you?");
}
}
With method m2() I do not get compiler error whereas with m1() I do. So this is my thinking and please correct me if I am wrong:

1) When an interface method declares that it throws an exception the implementing class can throw a narrower exception or not throw an exception at all.
public void m1()
{
System.out.println("Hi there");
}
does not give compiler error.

2) If implementing 2 interfaces with the same method but each one throws a different exception your implementing method can throw a more specific one if the exceptions have a subclass-superclass relationship.
Otherwise you can get a compiler error as is the case here because IOException and SQLException are not compatible with each other.
Your input will be greatly appreciated.

Thanks,
Meera
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your assumption is right here, but use the term checked and unchecked exception, as there is no such restriction for unchecked exceptions.
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the correction and your prompt reply Punit.
Meera
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is that there is no error regarding ambiguity .... both the methods have same nameas in the other interface . How is it resoved.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kedar Nath wrote:Why is that there is no error regarding ambiguity .... both the methods have same nameas in the other interface . How is it resoved.



Amibiguity happens where you cannot apply polymorphism such as fields, for instance methods polymorphism play important role, both interfaces method will be implemented by a single method, and it will suffice references of both the interfaces.


 
Kedar Nath
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So how can we get rid of the error in the above program??
Is it possible ? Without changing the method declarations in the interfaces??
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Use this one while implementing.
 
Kedar Nath
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. Now i am understanding.thanks
 
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic