Handling Known Errors and Error Messages in a Method
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?
In this case I would create an user defined exception called NewUserRegistrationException
with a special property (named Reason
) that would contain the reason of the failing.
Using your example, the enumerator
public enum RegistrationErrorType
{
Successful = 0,
EmailAlreadyExists,
UsernameAlreadyExists,
Etc
}
is pretty understandable.
Who then wants to register a new user by calling your method can just .ToString()
the exception to popup the generic error, or (after having read the documentation) switch
the Reason
property and react accordingly (focus on the email field, colour with red the password, etc).
Example code:
public class NewUserRegistrationException : Exception
{
public RegistrationErrorType Reason { get; private set; }
public NewUserRegistrationException(RegistrationErrorType reason)
: base()
{
Reason = reason;
}
public NewUserRegistrationException(RegistrationErrorType reason, string message)
: base(message)
{
Reason = reason; //might as well create a custom message?
}
public NewUserRegistrationException(RegistrationErrorType reason, string message, Exception inner)
: base(message, inner)
{
Reason = reason; //might as well create a custom message?
}
}
Whenever I start thinking about a common problem like this one the first thing I do is check if someone already came up with a good solution... so when a google search is performed for ".net validation framework" lots of good results pop up...
Lately I've been using TNValidate (http://tnvalidate.codeplex.com/)
精彩评论