• 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

Interface Using Exceptions

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Sir,

I have a query ...I Have read About Exceptions, I found that, If the Interface throws an exception (suppose SQL exception) then the class implementing the interface can throw only SQL exception only or it can throw other exceptions like IOException also. Is this rule applies to all the Classes. Can anyone Plase Show me the example of using Exception with Interfaces. Examples Helps Better to understand. I would be great full to you.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't you try it out yourself to see what works and what doesn't?

Suppose we have this:

And now we use this:

Note that service is of type MyDataService. The compiler knows that MyDataService.findPersons() can throw an SQLException, so it checks that you handle that exception. But the implementation that you use, MyDataServiceImpl, can also throw an IOException. The compiler can't check that, because service is declared to be of type MyDataService.

Can you see that that might be a problem?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The class that implements interface or class that extends another class must throw whatever exception thrown in base class or interface and can able to throw unchecked exceptions also.But should not throw checked exception if it is not found in parent class.It will tell compile time error.
reply
    Bookmark Topic Watch Topic
  • New Topic