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

context of RecordNotFoundException

 
Dmitri Christo
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In my specification (URLyBird 1.2.1) there isn't any description as to when to throw the RNFE. I am a bit skeptical regarding its use. For example the read method:
throws RNFE but when? Could be
1) When record is not found because recNo is invalid, or
2) When record is deleted?

My question is should the method return null instead of throwing an exception? If I must throw RNFE can it be for both the reasons above?

If this is left to the developer as a design decision, then I suppose another question can be raised with the find method: Most likely the find method will be implemented using read. Obviously a client performing a search can expect to have null returned, but would be ok for find to declare (or handle) rnfe?

Any advice on this is appreciated.
 
rohan tiwari
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I create a map of rec nos and file locations of valid records when my singleton data access class is instantiated.So if a rec no is not in this map, I throw rnfe.
There is a chance of rnfe in read also, but the contract says that read does not throw rnfe.So I handle rnfe in read.
 
Dmitri Christo
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes sense. I am doing something very similar. I just scan the db file and put all records in the map, so deleted entries might be there to, which means I have to check if the recNo requested is deleted or not.

So would you say that in my case throwing rnfe or returning null would be a design choice that just needs to be documented?

Thanks
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Dmitri.

Here's what I did: let's say that you call update(2, data) (where data is an array of String type), and there are 10 records in the DB file, but the first 9 records have the deleted flag. So only the 10th record is valid (so its number is 1). If you call update(2, data), I throw a RNFE, because in this case, there aren't 2 valid records. If you call update(1, data), it will update the first valid record (in this case, the 10th).

Have a good day, buddy!
 
Dmitri Christo
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your hint Bob. Sounds like a good idea. In my specification four of the methods that I need to implement throw rnfe. Since the instructions dont give more information, I figured something like the following:

1)readRecord: When recNo is invalid (i.e. out of bounds), or recNo points to a deleted entry
2)updateRecord: same as 1
3)deleteRecord: same as 1 - for example a record cannot be deleted twice.
4)lockRecord: When recNo is invalid - this method would be called when a client wants to eventually book/reserve a room, so he wouldn't be performing a lock to a deleted entry.

Also, within findByCriteria, I will handle rnfe in a try/catch block since findByCriteria will use readRecord to return any potential matches.

I hope something like the above is at least acceptable. Should a more 'sensible' or better way exists please share ideas.
 
rohan tiwari
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dimitri,

I add only valid records to my map, suppose there are 10 records (numbered 1-10) of which 1-7 are deleted or invalid so rec numbers 8,9,10 are only added to the map.hence, a read(4) would throw rnfe but a read(9) wont.

I just use containsKey to find whether the record exists in the database. I do not add rec numbers of deleted entries into the map since it is a sort of cache and I want to keep its size minimal.

Every time a create is done, the reused or new reoord number is added to the map.

Yes,you may document why you would throw rnfe rather than return null.
[ January 31, 2008: Message edited by: rohan tiwari ]
 
Dmitri Christo
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. So, it is mainly a design choice and if properly documented, it should be fine. Just wanted to know if some strategies are considered better that others.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dmitri Christo:
So, it is mainly a design choice and if properly documented, it should be fine.


That's exactly it, my friend. I chose to implement things this way because I found it easier; the record number 2 will always be the second valid record in a database file. But whatever is your implementation, just document it and you will be on your way to be a Sun Certified Java Developer!!!
 
Dmitri Christo
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your strong encouragement Bob!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic