Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about DBMain interface

 
Lara McCarver
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The specification for my DBMain interface says:

// Creates a new record in the database (possibly reusing a
// deleted entry). Inserts the given data, and returns the record
// number of the new record.
public int create(String [] data) throws DuplicateKeyException;


What is curious about this is that the data is supposed to be stored in a file. All the methods I have seen to write to a file will throw an IOException in certain error conditions. But according to this interface specification, the only exception I am permitted to throw is DuplicateKeyException. What am I supposed to do if the file system causes the write() method to throw an IOException? I want to return an accuarte error to the user, not DuplicateKeyException.

In the Data class, which implements this interface, I tried doing this:



but got an error because the interface that it is implementing only throws DuplicateException, not IOException.

I have similar problems with some of the other methods in this interface which produce IOExceptions.

Is it OK to change the interface to also throw IOException? And if not, how do you handle this type of error if it occurs?
[ May 07, 2005: Message edited by: Lara McCarver ]
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way is catch the IOException, and return an error value (e.g -1).
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One possibility is that any new exceptions you want to throw from the class that implements DBMain is to extend them from RuntimeException. RuntimeExceptions do not need to be declared in the throws clause of a method.

As long as you state in your choices that you had to implement them as RuntimeExceptions due to the limitation of the DBMain interface you should be fine.
 
Jared Chapman
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ditto what Matt said. That's how I handled it. But be SURE to document exactly why you did it that way, and other ways you considered.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic