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! Regards, Olena [ May 31, 2005: Message edited by: Olena Golub ]
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 ]