aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes urlyBird 1.2.3 - findByCriteria method 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 "urlyBird 1.2.3 - findByCriteria method" Watch "urlyBird 1.2.3 - findByCriteria method" New topic
Author

urlyBird 1.2.3 - findByCriteria method

Payal Shah
Ranch Hand

Joined: Jul 10, 2006
Posts: 67
Currently, if there is no match found, I return null. Now I am thinking i should throw RecordNotFoundException becasue that makes more sense.

Given method prototype does not throw any exceptions. I am planning to document either way I go.

Any suggestions??

Thanks
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2267
    
    3

My find method throws RecordNotFoundException if no records are found.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Alecsandru Cocarla
Ranch Hand

Joined: Feb 29, 2008
Posts: 158
You should not throw RecordNotFoundException if there are no matching records (especially if your method declaration does not allow it). This is a normal result of a search - no records found. It's not an error, and it's nothing exceptional about it.

Also, if I were you, I would also not return null. I would return an array of length 0 (zero).


SCJP 1.4 100%
SCJD 99.5%
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2267
    
    3

Alecsandru Cocarla wrote:You should not throw RecordNotFoundException if there are no matching records


I disagree. I think this is exactly the purpose of this exception in this case. And honestly, I think it is much better than to return a 0-length array, for instance. An exception is not necessarily an error. You are right Payal, it does make more sense.
Payal Shah
Ranch Hand

Joined: Jul 10, 2006
Posts: 67
Thank you all.

Just so much to do with this certification. Everytime I look the stuff I wrote, I want to change it to make it better or make it so it makes more sense.


Regards,
Alecsandru Cocarla
Ranch Hand

Joined: Feb 29, 2008
Posts: 158
Roberto Perillo wrote:I disagree. I think this is exactly the purpose of this exception in this case. And honestly, I think it is much better than to return a 0-length array, for instance. An exception is not necessarily an error. You are right Payal, it does make more sense.

I think it depends on what the outcome of this exception is. If you show it to the user like "you have an error - your search did not return any results", then I say (maybe) it should be an exception. But not necessarily (it is probably just a warning, or an invitation to search again).

If you use this exception just to show to the user an empty table, then it's incorrect - you're just using the exception for what? For nothing. It is completely equivalent to an empty array or list or whatever. It's a perfectly incorrect way of using exceptions.

If exceptions were so good when no records were found, they would have been used by relational databases and jdbc. They're not. Empty result sets are used for this very purpose.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2267
    
    3

Alecsandru Cocarla wrote:If exceptions were so good when no records were found, they would have been used by relational databases and jdbc. They're not. Empty result sets are used for this very purpose.


This made me think why this happens. On one hand, these are two completely different worlds. There are no exceptions in the relational databases world. One thing is OO and another thing is relational databases. Also, ResultSets return data (from the relational databases world), not entities (from the OO world). On the other hand, I looked at the JPA's javax.persistence.Query class API (which deals with entities), and saw that the getSingleResult method does throw a NoResultException if no results are found. The getResultList method does not throw any exception... so I guess it is what Andrew said on another post, different things can be right or wrong, depending on the point of view. But, since exceptions are not necessarily errors, and they exist to comunicate something to the upper layer or to the component calling a particular method, in my opinion, it is a good idea to throw a RecordNotFoundException if no records are found.
Alecsandru Cocarla
Ranch Hand

Joined: Feb 29, 2008
Posts: 158
Roberto Perillo wrote:There are no exceptions in the relational databases world. One thing is OO and another thing is relational databases. Also, ResultSets return data (from the relational databases world), not entities (from the OO world).

There are no exceptions, it's true, but there are error codes.

Roberto Perillo wrote:On the other hand, I looked at the JPA's javax.persistence.Query class API (which deals with entities), and saw that the getSingleResult method does throw a NoResultException if no results are found.

getSingleResult() throws NoResultException because the method clearly states what it should do: return one, and only one, single result. For the same reason, if there are more than one results, the method throws NonUniqueResultException. Indeed, in these cases, the absence or the multiplicity of results are errors and signaled appropriately.
Naveen Hegde
Greenhorn

Joined: Nov 28, 2006
Posts: 17
I strongly disagree that 'RecordNotFoundException' may be thrown when no record matchs search criteria. Always, find(criteria[]) operation can return a result array of size 0...n. i.e. the case of no records matching criteria is also a valid result.
I think exceptions can be thrown if no meaningful search can be performed on user input (criteria) OR 'find()' cannot be executed because of server connection problem etc..
The rule of thumb for me to decide whether to throw exception or not is 'can i proceed in normal execution path here after?'.
In searh case, code is executing in normal path till the last record is over. So i am not hitting the wall anywhere and hence return array of zero length.
In the UI also showing an empty table is sufficient with a status message like ' 0 of [Total] records found matching search criteria'. No need to even popup a warning/error dialog.


Thanks
---------------------------------------
Naveen Hegde, SCJP5.0, SCJD...
---------------------------------------
Alecsandru Cocarla
Ranch Hand

Joined: Feb 29, 2008
Posts: 158
Thanks, that was my point, too.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2267
    
    3

I disagree. I think it is a good idea to throw RecordNotFoundException. I think it makes the API easier for whoever is using this method. But again, both solutions are correct (throwing and not throwing the exception).
Mahesh Kumaraguru
Ranch Hand

Joined: Jun 01, 2005
Posts: 95
Article in Java.net discussing "Should you throw an exception, or return null?"

http://today.java.net/pub/a/today/2006/04/06/exception-handling-antipatterns.html

mohamed sulibi
Ranch Hand

Joined: Sep 04, 2005
Posts: 169
hi;

in my opinion you must throw the valid exception when the search don't match any records, but the RecordNotFoundException must be throws if the record doesn't found in the database but not for the records that don't match, and i think returning array with empty size or zero is the good and optimal solution for this case.


best regards.
Mohamed Darim.
SCJP, SCJD in progress ....
pramod karnani
Ranch Hand

Joined: May 22, 2007
Posts: 88

I think Alecsandru Cocarla is right we should not throw RNFE when find method returns array size 0.
And Roel De Nijs I need to tell you one thing that I am not deleting record physically from database file I am just changing deleted flag to 1 so it is possible criteria can match the deleted record .In that case find method will throw RNFE.


So what I conclude find method will throw RNFE only when criteria matches with any record in the database file and that records has deleted flag 1


So my assumption is okay?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5545
    
  13

Hi pramod,

I think cross-posting is not allowed, so i replied your post in the thread you started.

Regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: urlyBird 1.2.3 - findByCriteria method