Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3225914
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:23:41+00:00 2026-05-17T16:23:41+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:23:41+00:00Added an answer on May 17, 2026 at 4:23 pm

    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?
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What are some good ways to do this? Is it even possible to do
What are some good ways to catch business logic exceptions or return values from
Can anyone recommend some good resources that highlight the differences between Oracle and the
Does anyone know of some good resources related to setting up heirarchical user account
can you recommend some good ASP.NET tutorials or a good book? Should I jump
What are some good steps to follow for a smooth migration from PHP4 to
What are some good resources to learn best practices for Javascript? I'm mainly concerned
Can you suggest some good MVC framework for perl -- one I am aware
Does anyone have some good hints for writing test code for database-backend development where
Can anyone suggest some good browser add-on tools/extensions to help with development? I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.