• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface Using Exceptions  RSS feed

 
Abhinesh Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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?
 
Parvathi Raju
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!