• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

find() method and RecordNotFoundException

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have B&S application and a "clean" method signature for the "find" method that does not throw RecordNotFoundException.
The problem is as follows : while checking if the records are matching the given condition, it can be that a record is deleted and the RecordNotFoundException is thrown.
The solution that I have is to swallow the RecordNotFoundException and to ignore the record that was just deleted
Do you think that this is acceptable ? Does anybody have a better solution ?
Thank you very much in advance,
Liviu
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Liviu Carausu:
Hi all,
I have B&S application and a "clean" method signature for the "find" method that does not throw RecordNotFoundException.
The problem is as follows : while checking if the records are matching the given condition, it can be that a record is deleted and the RecordNotFoundException is thrown.
The solution that I have is to swallow the RecordNotFoundException and to ignore the record that was just deleted
Do you think that this is acceptable ? Does anybody have a better solution ?
Thank you very much in advance,
Liviu



I am also doing the Bodgitt and Scarper assignment, but my find method does throw a RecordNotFoundException like so:



In your case, you can handle a RecordNotFoundException inside your find method by logging a message indicating a deleted record. Swallowing an exception in your catch block and doing absolutely nothing is almost always bad practice and could cost you some marks.

Or, probably better, you could wrap the RecordNotFoundException inside a RuntimeException or other subclass (remember RuntimeException can be thrown without an exception declaration). The exception then is handled outside the find method, perhaps by showing a message to the user that the record, that is being searched by one user, was just deleted by another user.

Hope this helps. If anyone has a better solution or thinks I'm wrong, please correct me.
[ February 07, 2008: Message edited by: Aaron John ]
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi;

in my opinion also the find method throws RecordNotFoundException but in
the real life you not throw it , because it is your responsiblity to return all matching record. and your internel implementation not based on
index or something other than the sequentil data file.

so may be the evaluator but the RecordNotFoundException in the signature of
the find method , i think the find method must not throw RecordNotFoundException at ALL.

what you think.

Best Regards.
Mohamed Darim, SCJP, SCJD in progress ... ( upload 1/april/2008).
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you handle the cases where no records match the search criteria?
I would like to just return null, but the method signature could be interpreted like we should throw a RecordNotFoundException?
What do you think?
 
mohamed sulibi
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jim;

please don't return null , because the caller expect an array but you can return an empty array or list in bussiness class. so the caller can safty iterator on the return array or list.

for RecordNotFoundException your decision must be documented. but i think just returning empty array or list is enough tell about that.

regards.
 
Jim Petersson
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mohamed sulibi:
hi Jim;

please don't return null , because the caller expect an array but you can return an empty array or list in bussiness class. so the caller can safty iterator on the return array or list.

regards.



Hi Mohamed,
Yes that's true, thanks for pointing it out.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion, the exception should be thrown in the case search result is empty and follow the interface contract. That's the only reason for the exception there.
The caller is going to have a catch clause for that anyways.
 
mohamed sulibi
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jo�o Batista;


In my opinion, the exception should be thrown in the case search result is empty and follow the interface contract. That's the only reason for the exception there.
The caller is going to have a catch clause for that anyways.



you are right by following the interface contract BUT from my description regarding RecordNotFoundException "... should do so if a specified record does not exist or is marked as deleted in the database file.."

So this description is not true or clear about the job of find method.
also if the caller is catching the FNFE .. and the FNFE will not throw in find ! what is the problem ?

What you think ??

regards.
 
Jo�o Batista
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the delay..
What meant about following the contract is the "should do so if a specified record does not exist or is marked as deleted in the database file".

In the find method, the "specified record" is related to the criteria array. Hence, if no records are found (the specified record - though it could mean more than one), then the exception should be thrown.

At least that's my reading of it.
Hope it helps.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Regarding this post, I have a question and I am not how sure how to handle it

For the GUI search, it has a AND/OR requirement for hotel name and location (urlybird)

if find method from the Data class will throw RNFE, if no record match the criteria.
then the find method for Or, I am using 2 find methods from the Data class and union the result.
If one of the find method (e.g. hotel name) return a RNFE. I am thinking if i should swallow the exception. I agree that it is not a good idea to do nothing with the Exception caught.

How should I handle it ? as there may be match record for the other find method (e.g. location)
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my find method there is also no Record Not Found exception and there are comments above the method " A null value in criteria[n] matches any field value".

In my method I calculated total number of records then it checks in all not deleted records so I dont need Record Not Found exception for find method. But I used readRecord() mehtod inside the find() method, this readRecord() method throws RecordNotFoundException.

Secondly if null value is passed in method's array parameter i.e criteria[n] then all record number are returned as its asked in methods signature. Then on client side it does filteration again and passes the exact matched records in JTable. If no match found then only fields names as headings appear at the top of JTable.

Best Regards!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic