File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Question about DBMain interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Question about DBMain interface" Watch "Question about DBMain interface" New topic

Question about DBMain interface

Lara McCarver
Ranch Hand

Joined: Dec 09, 2003
Posts: 118
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

Joined: Jun 25, 2004
Posts: 119
One way is catch the IOException, and return an error value (e.g -1).

Current Status:<br /> <br />SCJP 1.4<br />SCJD (in progress)
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Feb 23, 2004
Posts: 81
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.

B.S. University of Wisconsin<br />SCJP 1.4 (85%)<br />SCJD 1.4 (92%) B&S Contractors
I agree. Here's the link:
subject: Question about DBMain interface
It's not a secret anymore!