I have a function that requests a person, returning the person ID. However, if method detects a fault, I want to return an error (With a description?).
So, lets say my function is:
public int GetPersonId(string username)
{
//Logic
return personId;
}
Within the logic, I call a proc, and return an ID. However, if no results were returned, I could return -1, and handle that in the calling code – but feel this isn’t good.
Would it be better the create an exception, and try/catch it, or what? I’m sure I read once, that throwing exceptions for business type rules, isn’t good practise.
What’s the best way to deal with this.
Additionally, maybe the proc will return other statuses, such as ‘person exists, but is marked as deleted’, ‘no such person exists’ etc. What I mean is there is more than one ‘exception’.
I think that you need to distinguish very clearly what is an error in your function and what is not. If the fact that no results are found is an error (only you can define if it is or not), then throw an exception. If it is not an error (because is a possible output), then return a code or whatever you want. The calling code should manage it. But don’t use exception handling for dealing with the logic of a program. It is incorrect