A lot of the code base methods my peers have written perform their own error handling, usually by catching, notifying, and logging.
In these cases the methods return a boolean, indicating success or failure.
Sometimes though, if a method fails, I want the calling code to know why, and returning a boolean is not enough.
One way around this is to keep the error handling in the methods, but make the method void, and have the methods throw their Exceptions.
However, recently I've got into the habit of returning Exceptions in appropriate methods, usually action methods that could otherwise be void, but where I'd like to know the status of the action.
The advantages of returning an Exception over having a void method throw an Exception is that I think it makes it easier for other programmers to use your library, and, more importantly, it means you can safely call that method without having to worry about catching the Exception
For example, if the method is just void, it might not be instantly obvious that the programmer should handle success or failure.
But if the methods specifically specifies a return type of Exception, then you know you can check success or failure if you want to. But it also means you don't need to worry about catching the error if you don't want to.
Does that make sense? Maybe I haven't used the best example, but generally, is it OK to return Exceptions, or is there a better pattern out there?
cheers
UPDATE
wow, the overwhelming result is no way. I thought so. I must say, doing it (returning an Exception) kinda solved a problem, but it did feel wrong.
So, from some of your answers, the best solution to these specific scenarios (a complex class, or a class with one or more external dependencies (i.e. web service)) seems to be a custom results Class?
UPDATE:
hey guys, really appreciating all the opinions, am reading through everything, and I'm thinking carefully about all the input.
Currently I'm favoring having a void method, throwing the Exceptions, and then catching them on the outside....is that better?
cheers