• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

RecordNotFoundException

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 284
Netbeans IDE Firefox Browser Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic