Last post Nov 28, 2013 11:23 AM by Siva Krishna Macha
Nov 28, 2013 10:44 AM|mayurarora|LINK
I am stuck in a scenario where I have to add a try-catch block in a method and the try block includes the entire method definition; including the return statement. I was just wondering, if my method returns a non-void value and the return statement is a
part of the try block; the catch block should also have a return value. But what do I return ?
Is it just about returning an empty string, an instance of a class or a primitive value ? Is it just about returnig something that is expected from the method ?
I feel as if I am missing a point here.
Need some help with this.
Maybe, the return statement should not be part of the try block. It should come after the catch block.
Nov 28, 2013 11:07 AM|PatriceSc|LINK
What do you do in your catch block ? More likely you should wrap the exception and throw another one. If you don't then one could get the returned value whatever it is and use that without knowing that something wrong happened.
Nov 28, 2013 11:23 AM|Siva Krishna Macha|LINK
YourReturnType YourMethod(input xyz)
YourReturnType retVal = null;
// YourLogic here
retVal = <soAndsoObject>;
// Handle your exception or, you can even throw exception here..
Try it as above. Hope it helps.