Forums Register Login

adding exceptions to DBAccess

+Pie Number of slices to send: Send
Hi,

My Data class which implements DBAccess throws exceptions which are not listed in the interface. For example, the createRecord() method throws IOException, but the interface only declares DuplicateKeyException to be throwable.

I've been reading about possible solutions, and have identified 3:

1. Chain the IOException to the DuplicateKeyException

Personally, I don't think this is at all appropriate in this case because I believe that two exceptions should only be chained if they are related. For example one might want to chain a very specific exception to a more general exception (and throw the latter).

2. Chain the IOException to an unchecked exception

I don't like this the idea of mixing together checked/uncecked exceptions. Also, a book I've read says that you should never throw unchecked exceptions yourself, as they are indicative of a programming error.

3. Return an error code

Rather than throwing an exception to indicate an error, catch the exception and return an error code, e.g. -1 in the case of createRecord. This seems the best of a bad lot to me, but is a procedural rather than OO technique.


I'd be interested to hear others think is the best solution to this problem.

Cheers,
Dan
+Pie Number of slices to send: Send
Error codes are the worst of this bad lot. My choice was to use an unchecked exception and I got full marks for data store and OO design.

The essense of an unchecked exception is not that it is a programming error, but rather that it is unrecoverable, at least in the layer that threw it. What I did was to wrap all IOExceptions, RemoteExceptions and any new exception that I needed in the Data class in a DataException. This was thrown by the database server and caught by the business logic, which then found the cause and reported it to the user, allowing the user for example to pick a different database file or server.
Seriously Rick? Seriously? You might as well just read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 580 times.
Similar Threads
B&S: Exceptions Problem
B&S 2.2.2 - Network Layer Query
NX: 5 problems about Data.java
NX: Exception handling implementing the DBAccess
NX: Question on Max's DVD project
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:05:16.