Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returned data

 
Stuie Clarky
Ranch Hand
Posts: 98
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Tony Docherty
Bartender
Posts: 2957
59
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 98
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic