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

  • SEARCH
  • Home
  • 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 8397637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:50:28+00:00 2026-06-09T20:50:28+00:00

When throwing exceptions in the following auth class example is it recommended to throw

  • 0

When throwing exceptions in the following auth class example is it recommended to throw a different exception for each situation to be handled, eg:

addUser(...) {

    // normal database code here...

    switch(TRUE) {
        case ($username_exists):
            throw new UserExists('Cannot create new account. Account username ' . $un . ' already exists.');
        case ($email_exists):
            throw new EmailExists('Cannot create new account. Account email ' . $email . ' already exists.');
    }
}

   //to be called externally by...

try {
    $auth->adduser(...);
} catch (UserExists) {
    $output = 'That username is already taken.';
} catch (EmailExists) {
    $output = 'That email is already being used.';
} catch (AuthException $e) {
    $output = $e->getMessage();
}

echo $output;
}

or is it recommended to throw a general “type” of exception with a unique exception code? eg…

addUser(...) {

    // normal database code here...

    switch(TRUE) {
        case ($username_exists):
            throw new AuthException('Cannot create new account. Account username ' . $un . ' already exists.', 10);
        case ($email_exists):
            throw new AuthException('Cannot create new account. Account email ' . $email . ' already exists.', 20);
    }
}

   //to be called externally by...

try {
    $auth->adduser(...);
} catch (AuthException $e) {
    switch($e->getCode()) {
        case 10:
            $output = 'That username is already taken.';
            break;
        case 20:
            $output = 'That email is already being used.';
            break;
        default:
            $output = $e->getMessage();
    }
echo $output;
}

I ask because I’m new to exceptions and both solutions seem equally viable. Perhaps there are other solutions entirely?

Interesting side-note: I didn’t realize I was asking a “what’s your preference” question until after receiving a few answers. I anticipated a single recommended method.

  • 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-06-09T20:50:30+00:00Added an answer on June 9, 2026 at 8:50 pm

    I wouldn’t say one way is right and one way is wrong – and asking a “is this way recommended versus this way” can easily stir up a large pot if the question hits the right nerve in a large group.

    Regarding your question specifically, both are valid and acceptable ways to throw different types of exceptions – and I’ve seen both quite frequently.

    In large-scale applications in just about every language, however, I see the bottom method more often than not – and it’s also my own personal style/preference. I think that throwing a single “type” of exception, AuthException, and specifying an exception-code is very clear and concise.

    If you want it to be more descriptive (from a programming standpoint), you can use an pseudo-enum* setup for the codes to give a user-friendly description:

    class AuthExceptionCode {
        const USER_EXISTS = 0;
        const EMAIL_EXISTS= 1;
        // ....
    }
    

    To throw the exception with the code:

    throw new AuthException('error message', AuthExceptionCode::USER_EXISTS);
    

    If you have an actual custom-Exception class, i.e. – you extend Exception, you can place the codes direction in the class itself:

    class AuthException extends Exception {
        const MISC = -1;
        const USER_EXISTS = 0;
        const EMAIL_EXISTS= 1;
    
        public function __construct($message, $code = self::MISC) {
            switch ($code) {
                case self::USER_EXISTS:
                    // example on how to access the codes from within the class
                    // ...
    

    * Enumerations are not native to PHP, so using const is the easiest method (without using a 3rd-party class/plugin.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Throwing exceptions often follows the following pattern: if(condition) { throw exception; } you check
Simple Inject is throwing the following exception when attempting to register my DbContext. The
My application is throwing a Security Exception with the following details: Description: The application
In the following code t.schedule(timertask, d.getDate(), 1000); is throwing NullPointer exception help me out
My code is throwing the above exception on the following line (line 2 of
I have a custom sharepoint web part that is throwing the following exception: System.Security.SecurityException:
The following code is throwing exception because string field has the SQL reserved word
Given that eating exceptions is always bad juju and re-throwing the exception loses the
I am unable to convert strings to date with throwing exceptions of not proper
I'm working with the Apache Thrift tool and i'm throwing some Exceptions from the

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.