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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T07:57:11+00:00 2026-05-17T07:57:11+00:00

I’m not very familiar with machine code, but I think this is a pretty

  • 0

I’m not very familiar with machine code, but I think this is a pretty simple question.
If I want to do error handling via an integer returned from a function (as opposed to the function throwing an exception), is it better practice—from a machine code standpoint—to:

  1. Check the integer in a conditional statement for a “bad” value, and then use a switch statement to handle the “bad” value(s), or
  2. Switch the integer, and provide a case for the “good” value(s) as well as the “bad” value(s)

For example, in C++:

enum error_code {E_GOOD, E_BAD, E_UGLY};
error_code func_b();

Option 1

void func_a()
{
    error_code err_catch = func_b();

    if (err_catch)
    {
        switch (err_catch)
        {
        case E_BAD:
            /* Handle bad case */
            break;
        case E_UGLY:
            /* Handle ugly case */
            break;
        }
    }
}

Option 2

void func_a()
{
    error_code err_catch = func_b();

    switch (err_catch)
    {
    case E_GOOD:
        break;
    case E_BAD:
        /* Handle bad case */
        break;
    case E_UGLY:
        /* Handle ugly case */
        break;
    }
}

Thank you for your help.

  • 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-17T07:57:11+00:00Added an answer on May 17, 2026 at 7:57 am

    The first test should probably be:

    if (err_catch != E_GOOD)
    

    It is explicit about ‘an error occurred’. Incidentally, your code looks like is passing through a C++ compiler. C does not create a type error_code with the preceding enum; you would have to add:

     typedef enum error_code error_code;
    

    There will be very little difference between the two in terms of generated code.

    I would probably use Option 1 (the if notation) simply to make it clear that the switch only deals with error cases (because it only has to deal with error cases), but I wouldn’t object to either if it was presented to me for code review.

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

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I
I have just tried to save a simple *.rtf file with some websites and

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.