What are some good ways to handle known errors that occur in a method?
Let's take a user registration method as an example. When a user is signing up, a method SignUp( User user )
is called. There are a few known errors that might happen.
- Email is already registered
- Username is already registered
- Etc
You could throw specific exceptions:
public void SignUp( User user )
{
// Email already exists
throw new EmailExistsException();
}
Now specific exceptions could be caught.
This is bad in my opinion, because exceptions are being used for flow control.
You could return a boolean stating if it succeeded and pass in an error message that would get set if an error occurs:
public bool SignUp( User user, out/ref string errorMessage )
{
// Email already exists
errorMessage = "Email already exists.";
return false;
}
I don't like this for a few reasons.
- A value has to be returned. What if the method needs to return a value?
- An error message has to be passed in every time.
- The consumer of the method should be the one determining what the message is.
Let's just say anything where the actual message set in the method is bad.
You could use error codes:
public enum Errors
{
Successful = 0,
EmailExists,
UsernameExists,
Etc
}
public Errors SignUp( User user )
{
// Email already exists
return Errors.EmailExists;
}
// or
public void SignUp( User user, out/ref Errors error )
{
// Email already exists
error = Errors.EmailExists;
}
The very last one here is the one I like best, but I still don't like it a whole lot. I don't like the idea of passing an error code in. I don't like the idea of returning an code either, for that matter.
I like the idea of using custom exceptions because it seems a little cleaner, but I don't like the idea of using exceptions for flow control. Maybe in specific cases like this example, an email already being in the system SHOULD be an exception, and it's ok.
What have other people done in this situation?