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 6158001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:52:53+00:00 2026-05-23T20:52:53+00:00

Is there a right way to define error codes? I mean, I’ve built a

  • 0

Is there a “right way” to define error codes? I mean, I’ve built a library a while ago that throws custom exceptions, but I targeted the custom error messages to a developer’s standpoint. Now I’m wrapping up the GUI and when I catch those exceptions, I need more user friendly messages. That’s not a problem in itself, but let’s say, I have my ReceiverNotAvailableException exception and NoMessageReceivedException. To me, as a developer, they mean completely different things and have different inner messages, but to the end-user they just mean “User not found”. I’d like to display something like “User not found (error X)” where X varies depending on which exception is raised – pretty commonplace if you ask me.

My question is: should I go with X=1, 2 and so forth depending on what kind of exception or should I opt for something more complicated for whatever reason? I know it sounds like a dumb question, but I’d really like to know what the “best practice” (I’m not so fond of the term) is in this case.

BTW, of course I’d have a table mapping each code to its corresponding exception, whichever the case is.

  • 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-23T20:52:53+00:00Added an answer on May 23, 2026 at 8:52 pm

    If your exceptions can’t overlap, then going with a HashTable[ExceptionName] = “Error Message” looks like a sane option. If they can you can use something like the following:

    The standard way to define message codes that can overlap (this is, occur at the same time) is to use powers of two:

    define ERROR_SYSTEM_DOWN  1
    define ERROR_DATABASE_UNREACHABLE 2
    define ERROR_SPACE_UNAVAILABLE 4
    define ERROR_DISK_DIED 8
    

    and so on. Then, in code you can do

    if (disk_died() && no_space()) {
        int errorCode = ERROR_DISK_DIED | ERROR_SPACE_UNAVAIABLE; //Binary or
        return errorCode;
    }
    

    Finally, on the receiving end you can:

    if (errorCode & ERROR_DISK_DIED == ERROR_DISK_DIED) { //Binary and
        //then, at least, disk died. You can check for the rest in the same way
    }
    

    Explanation:

    ERROR_SYSTEM_DOWN = 0001
    ERROR_DATABASE_UNREACHABLE = 0010
    ERROR_SPACE_UNAVAILABLE = 0100
    ERROR_DISK_DIED = 1000
    

    Then

    1000 | 0100 = 1100
    

    and, on the checking code

    1100 & 0100 = 0100
    

    Now, if you are using exceptions you can use the same approach, bubbling up the errorCode as long as exception occur. Although this idiom is more common in C.

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

Sidebar

Related Questions

That's basically the question, is there a right way to implement operator<< ? Reading
What is the right way to perform some static finallization? There is no static
Is there a way to detect a right click followed by paste with JavaScript
Is there a way to define the following structure in a DataContext/DBML file? public
is there any way of concatenation #define macros in c++ (ms compiler)? I have
I realize there's no definitely right answer to this question, but when people talk
Are there any decent PHP libraries available for accessing SVN repositories? Right now I
For example, if you type something in upper-right google/yahoo search box in firefox there
There is a conversion process that is needed when migrating Visual Studio 2005 web
I am working on two wrapper classes that define real and complex data types.

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.