• 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

Returned data

 
Ranch Hand
Posts: 104
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Quick question regarding returned data from a method call. In this example, I am checking to see if a database record exists. As long as it exists then I am happy, however if it does not then I will catch the exception and display the error and required validation message to the user. My question is, is it good practice to ignore returned data, or to return it to an object that has no use?

Cheers,

S
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say it's generally not good practice to call a method to see if an exception will be thrown or not. Exceptions are supposed to be for exceptional circumstances and not to test the state of something.
Without more details about what you are doing and what code you are using it is hard to tell but it sounds like a design issue to me. You probably need to add a specific method eg hasRecord() to test if the record exists and return a true or false result.

If, on the other hand, the only way you have of checking is via the method that returns data or throws an exception wrap the call to it and associated exception handling in your own hasRecord() method which returns a boolean.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:If, on the other hand, the only way you have of checking is via the method that returns data or throws an exception wrap the call to it and associated exception handling in your own hasRecord() method which returns a boolean.


I totally agree with your general advice and would only add that something like that can probably be achieved most efficiently with a 'SELECT COUNT(*) FROM...' statement, in which case you can just have the custom method return the result as an integer.

Winston
 
Stuie Clarky
Ranch Hand
Posts: 104
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers for the replies guys. The method originally described was the first look as I started this bit of work just before going home :P I'll look into creating a more explicit method to perform the check rather than hacking the existing CRUD get method.

Thanks,

S
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic