• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RecordNotFoundException

 
Sophie Jane
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Really quick question. Within my URLyBird spec it says "Any methods that throw RecordNotFoundException should do so if a specified record does not exist or is marked as deleted in the database file. "

Therefore, within my readRec(long recNo) method, I check the validFlag field and if not equal to zero I throw a new RecordNotFoundException.

However, I have just completed the findByCriteria method, and I basically loop through each if the records. However, if any if the records I loop through are deleted I will get a "RecordNotFoundException" that will terminate my search across the file.

While coding, I put in the following

for loop that iterates through all rec
{
try
{
rec = readrec(index)
}
catch( RecordNotFoundException rnf )
{ continue; } //Check next record

for loop that iterates through all fields
{
}
}

My question is...Is the "continue" in the catch block bad programming practice? At the time, it was a hack, but the more I think about, I would prefer maybe to write another readRec that did not throw a RecordNotFound exception? But then duplicate code essentially.

It would be great to hear peoples opinions on this?
Thanks
Sophie
 
Robert Bar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sophie,

you don't have to duplicate code. Try implementing isValid() operation:



then your find() code will look like this one:



or



isValid can be used to check whether RecordNotFound must be thrown, for example:



regards,
Robert
 
Oricio Ocle
Ranch Hand
Posts: 284
Debian Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sophie,
this thread could be interesting for you:
Why not come in and debate on my code of find?
Regards
 
Jason Moors
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative which I believe Oricio also mentions in his post is to create a private getRecord method which doesn't throw an exception.

You can then use the private getRecord method in you find method without needing to catch any expections, and the public instance methods can also use the private getRecord method to retrieve the record and then throw exceptions where required.

I believe this approach, not only makes the code easier to read, but also improves performance as you are not throwing expensive checked exceptions.

Hope it helps

Jason
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic