Today I had to make a decision whether I should be throwing an exception or not.
In my data access layer I had to have a SaveUser method that saves a user object by calling a corresponding SaveUser method on a REST web service. The question arose regarding the return value of the method in my data access layer. Logically the return value should be a boolean, either the SaveUser operation succeeded or not.
Here is one possible method signature:
bool SaveUser(User user); // not such a good approach
However, this is not a good solution. Why? The return value should always be true, and false only in exceptional circumstances.
In general, when saving data, it is much better to return nothing, and let exceptional situations be handled by the best candidate - exceptions.
Therefore, the SaveUser method should have the following signature:
void SaveUser(User user); // much better approach
If the web service returns an error, the data access layer should raise an exception and set the InnerException propety to the exception text returned by the web service.
This is a much cleaner solution than returning true/false from the SaveUser method.