• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pointless DuplicateKeyException exception?

 
Paul Bilokon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure whether anyone sitting SCJD has encountered this before. If this issue has already been raised (I couldn't find any related questions), my sincere apologies.

As usual, there is a flat file database. In my case, hotel room reservations. The records are identified by their numbers, recNo. Now, here is what I'm finding strange.

The interface DB has the following member method:

* public int create(String[] data) throws DuplicateKeyException;

The specification does not mention the semantics of the
DuplicateKeyException thrown by this method, although its name seems to
suggest the existence of a certain "key", which must necessarily be unique,
since the existence of a "duplicate" is regarded as an exceptional
situation.

Moreover, it appears that this key consists of a subset of the fields
comprising the data parameter. These fields are, as per the Data File
Format section of the Instructions, (name, location, size, smoking,
rate, date, owner). The question is, which subset?

(name, location) appears to be a natural choice for the key. However, it is
not inconceivable (on the contrary, highly likely) that Dew Drop Inn (name)
in Smallville (location) has more than one room to offer. Hence the
non-uniqueness of the key. Likewise, the existence of two rooms with
identical (name, location, size, smoking, rate, date) is neither
impossible, nor unlikely.

A single person could not occupy multiple rooms. Notice, however, that
owner is defined as "the id value [...] of the customer who has booked
this [room]", not the occupant of the room. Hence it is possible to have
two rooms with identical (name, location, size, smoking, rate, date,
owner), distinguished by their recNo's alone.

If my reasoning is correct, the DuplicateKeyException should never be thrown. Moreover, since this is its sole occurrence in the specification, the exception class itself is redundant.

Would anyone kindly correct me if I'm wrong?

Best wishes
 
Cindy Rogers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, search this forum for "DuplicateKeyException" to view several other similar questions.

I'll copy Andrew Monkhouse's response to one of them:
"Just because a method signature declares that a particular exception might be thrown does not mean that you have to throw it. If you cannot find a unique key in the data you have been provided (and from memory you cannot get a unique key in URLyBird) then you may want to just consider it as something intended for future use."

Just remember to document your decision and rationale...
 
Paul Bilokon
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cindy,

Many thanks for your reply. My original intention was to write something to that effect in choices.txt. Still, I think that including a redundant exception is somewhat dubious, but I can see how the examiners could be expecting to see "does not mean that you have to throw it" and "intended for future use".

Once again, thank you,

Paul
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic