aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes find() method and RecordNotFoundException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "find() method and RecordNotFoundException" Watch "find() method and RecordNotFoundException" New topic
Author

find() method and RecordNotFoundException

Liviu Carausu
Ranch Hand

Joined: Oct 07, 2004
Posts: 158
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


Oracle Certified Master Java SE6 Developer(SCJD),
OCE JEE 6 JSP and Servlets Developer.
Aaron John
Ranch Hand

Joined: May 30, 2005
Posts: 74
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 ]
mohamed sulibi
Ranch Hand

Joined: Sep 04, 2005
Posts: 169
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).
Jim Petersson
Ranch Hand

Joined: May 28, 2008
Posts: 48
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?


SCJP 5<br />SCJD
mohamed sulibi
Ranch Hand

Joined: Sep 04, 2005
Posts: 169
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

Joined: May 28, 2008
Posts: 48
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.
Jo�o Batista
Ranch Hand

Joined: May 25, 2008
Posts: 40
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

Joined: Sep 04, 2005
Posts: 169
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

Joined: May 25, 2008
Posts: 40
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.
Heilien Tsui
Greenhorn

Joined: Dec 08, 2008
Posts: 20
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)


SCJP 5, SCJD 5, SCWCD 5, SCDJWS 1.4, SCBCD 5, SCEA 5
Tahir Abbas
Ranch Hand

Joined: May 05, 2007
Posts: 44
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: find() method and RecordNotFoundException