• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Question about DBMain interface

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way is catch the IOException, and return an error value (e.g -1).
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic