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

urlyBird 1.2.3 - findByCriteria method

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My find method throws RecordNotFoundException if no records are found.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Roberto Perillo
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Alecsandru Cocarla
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, that was my point, too.
 
Roberto Perillo
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
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 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 ....
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi pramod,

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

Regards,
Roel
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic