Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

adding exceptions to DBAccess

 
Dan Murphy
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic