Handling known errors and error messages in a method

What are some good ways to handle known errors that occur in a method?

Take the user registration method as an example. When a user logs in, the SignUp( User user ) method is SignUp( User user ) . Several known errors are known.

  • Email is already registered
  • Username already registered
  • Etc

You can specify specific exceptions:

 public void SignUp( User user ) { // Email already exists throw new EmailExistsException(); } 

Certain exceptions can now be detected.

This is bad, in my opinion, because exceptions are used to control the flow.

You can return a logical message if it was successful, and send an error message that will be set if an error occurs:

 public bool SignUp( User user, out/ref string errorMessage ) { // Email already exists errorMessage = "Email already exists."; return false; } 

I do not like this for several reasons.

  • Value must be returned. What if the method should return a value?
  • An error message should be transmitted at any time.
  • The consumer of the method should be the one who determines what the message is.

Let me just say something where the actual message set in the method is bad.

You can 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 most recent of them is the one I like best, but I still don't like it. I don't like the idea of ​​passing an error code. I also don't like the idea of ​​returning the code, 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 to control the flow. Perhaps in specific cases like this example, an email already in the system SHOULD be an exception, and that’s normal.

What have other people done in this situation?

+6
c #
source share
2 answers

In this case, I will create an exception, which is indicated by the user NewUserRegistrationException with a special property (named Reason ), which will contain the reason for the failure.

Using your example enumerator

 public enum RegistrationErrorType { Successful = 0, EmailAlreadyExists, UsernameAlreadyExists, Etc } 

pretty clear.

Who then wants to register a new user by calling your method can simply .ToString() exception to pop up a message about a general error or (after reading the documentation ) switch Reason properties and react accordingly (focus on the email field, color with a red password, etc. d.).

Code example:

 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? } } 
+2
source share

Whenever I start thinking of a general problem like this, the first thing I do is to check if someone really came up with a good solution ... so when google search is done for the ".net validation framework", a lot of good results pop up ...

I have been using TNValidate recently ( http://tnvalidate.codeplex.com/ )

0
source share

All Articles