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 Exceptions handling during find operation. 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 "Exceptions handling during find operation." Watch "Exceptions handling during find operation." New topic

Exceptions handling during find operation.

Olena Golub
Ranch Hand

Joined: Jan 17, 2005
Posts: 113
Hello Everyone,

I have no any experience with the exception handling and I have some problems and questions about it. Could you help me?
How do you deal if the exception occurs during the find method?
In SUN�s Interface there are no exceptions defined in the find method. Do you throw the subclass of the RuntimeException or do you ignore it, if some exception occures in find() method?

For example, what do you do, if some IOException occures during the reading of all record for further checking of the record with criteria? Or after you read all records and no record was read from the database.
How do you handle it?

In Data I have two methods find(�) (original from SUN) and a private one getAllRecords(). In the find method I call getAllRecords() method, to receive all records from the database.

Thanks a lot for your help!
[ May 31, 2005: Message edited by: Olena Golub ]

SCJP 1.4<br />SCJD 1.4 (in progress)
Ken Boyd
Ranch Hand

Joined: Dec 10, 2003
Posts: 329
If you are calling read(recNo) method which will throw RecordNotFoundException in case it found no record

John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Here are a few sensible rules to follow with exceptions:

  • If you can do some meaningful exception handling within the method that throws an exception, handle it there, otherwise throw.
  • Throwing RecordNotFoundException of perhaps runtime DatabaseException is certainly better than just IOException or worse, just Exception. The more specific, the better, because the catcher will have more knowledge of how to process it.
  • Throw exceptions appropriate to the abstraction. For example, it makes sense to throw a RecordNotFoundException from the read(recordNumber) method (in case the record number is out of range or perhaps the I/O problem occured), but it doesn't make sense to throw it from the find(criteria) method.
  • Ignoring the exception is the worst type of programming. Aside from logging an exception, your code

  • ... simply goes on with the excecution, and is likely to cause all sorts of problems (the eventual NullPointerException is highly likely).

    In your specific case, since there is nothing you can do if there is a database reading error, my recommendation is to map the RecordNotFoundException into a runtime DatabaseException (since you probably don't want to change the signature of the find(criteria) method and allow it to propagate up the stack. Somewhere up in you controller (if you are using the MVC pattern), you would eventually catch it and display a message to the user (not just to the logger), such as "Unexpected database error", "Could not complete this transaction because the database is corrupted", or "Database error. Please contact your system administrator".
    [ May 31, 2005: Message edited by: John Smith ]
    I agree. Here's the link:
    subject: Exceptions handling during find operation.
    It's not a secret anymore!