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?
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.
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.
Isn't it funny how there's always time and money enough to do it WRONG?
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.