aspose file tools*
The moose likes Beginning Java and the fly likes Returned data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Returned data" Watch "Returned data" New topic
Author

Returned data

Stuie Clarky
Ranch Hand

Joined: Nov 09, 2012
Posts: 76

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


OCAJP 7
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2398
    
  50
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

Joined: Mar 17, 2011
Posts: 8404
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stuie Clarky
Ranch Hand

Joined: Nov 09, 2012
Posts: 76

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returned data