Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Known Errors and Error Messages in a Method

Tags:

c#

.net

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?

like image 413
Josh Close Avatar asked Oct 13 '10 16:10

Josh Close


People also ask

What are the error handling methods?

There are four ways to handle errors in Swift. You can propagate the error from a function to the code that calls that function, handle the error using a do - catch statement, handle the error as an optional value, or assert that the error will not occur. Each approach is described in a section below.

What is error and error handling?

What Does Error Handling Mean? Error handling refers to the response and recovery procedures from error conditions present in a software application. In other words, it is the process comprised of anticipation, detection and resolution of application errors, programming errors or communication errors.

Why do you think we should handle errors and exceptions?

One reason you need standards for exception handling, and don't want to needlessly pursue them all, is that certain exceptions can corrupt data and leave your application in an even worse error state.

How do you handle error messages in Java?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.


1 Answers

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?
    }
}
like image 87
Alex Bagnolini Avatar answered Oct 14 '22 09:10

Alex Bagnolini