Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DBMain interface - Exception issue

 
Mike Tilling
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my assignement (UrlyBird), the Data class must implement DBMain interface, the later contains (for example) the method :

public String[] read (int recNo) throws RecordNotFoundException

I can not implement the read method in the Data class without throwing IOException,so, the compiler will raise the error : "overriden method does not throw IOException.

-can I change the method signature in the DBMain interface to include IOException in the throw clause; If not, how can I deal with this issue?

Best Regards
 
Jason Moors
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,

I wouldn't recommend changing the method signature. There are a number of posts on the forum regarding exceptions.

http://www.coderanch.com/t/188303/java-developer-SCJD/certification/RecordNotFoundException-dilema-pease-help
http://www.coderanch.com/t/187886/java-developer-SCJD/certification/DBMain-New-Exceptions
http://www.coderanch.com/t/187937/java-developer-SCJD/certification/Declaring-RuntimeExceptions

I think there are two possible solutions, both have disadvantages.

Use Chained Exceptions - Catch the IOException and wrap the exception in the RecordNotFoundException, or subclass of RecordNotFoundException.

Use Unchecked (Runtime) exceptions which don't need to be declared as part of the method signature.

Which ever way you go I think you have to make a compromise, I personally documented that the signature was insufficent, and went for the unchecked approach as I felt that most of the errors that are not catered for are fatal errors, like not being able to read the file etc and I also considered chaining IOExceptions into sub classes of RecordNotFoundException to be more than confusing.

I have just made sure that I have provided very extensive documentation for methods that throw my runtime exceptions.

regards
Jason
 
Mike Tilling
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Use Unchecked (Runtime) exceptions which don't need to be declared as part of the method signature."

Thnaks Jason

Do you mean that I catch IOException then I throw a RuntimeException (say DatabaseIOException that Extends RuntimeException)

Thanks in Advance
Best Regards
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, wrap the exception in a RuntimeException of some sort and throw that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic