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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:23:15+00:00 2026-06-01T22:23:15+00:00

Sometimes when I am programming in C++/C I end up calling the same function

  • 0

Sometimes when I am programming in C++/C I end up calling the same function multiple times and I was wondering what is the most efficient way to check for errors for all of those calls? Using if else statements take up a lot of code and look ugly. I have come up with my own way of checking for errors, perhaps there is a better way that I should use.

int errs[5] = {0};
errs[0] = functiona(...);
errs[1] = functiona(...);
...
errs[5] = functiona(...);
for (int i = 0; i < 5; i++)
{
  if (err[i] == 0)
     MAYDAY!_wehaveanerror();
}

Note: I understand that using try and catch might be better for C++ as it would solve this problem by throwing an exception on the first error, but the problem with that is that it is not compatible with a lot of functions that return error codes such as the Windows API. Thanks!

  • 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-01T22:23:16+00:00Added an answer on June 1, 2026 at 10:23 pm

    If…IF the function has a chance to throw a different error you should also add a catch all.

    struct my_exception : public std::exception {
        my_exception(int); /* ... */ };
    
    int main()
    {
        try
        {
            int e;
            if ((e = function()) != SUCCESS) { throw my_exception(e); }
            if ((e = function()) != SUCCESS) { throw my_exception(e); }
            if ((e = function()) != SUCCESS) { throw my_exception(e); }
        }
        catch (my_exception & e)
        {
            std::cerr << "Something went wrong: " << e.what() << "\n";
        }
        catch (...)
        {
            //Error Checking
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am programming a game since 2 years ago. sometimes some memory errors (ie:
I'm having the weirdest problem ever when programming in C. My function sometimes runs
i have been programming for sometime but all of my programming books have not
I use Eclipse for programming in PHP and Java(Android) and sometimes Python, unfortunately Eclipse
Sometimes procedural programming is absolutely unavoidable when processing data. I am currently working on
Programming isn't my main job, though I enjoy it and sometimes get paid for
One of the most common mantras in computer science and programming is to never
Over the course of programming I get errors that give a resource number like
When programming with c-style i/o I sometimes use freopen() to reopen stdin for testing
I'm programming with Java in Linux using Netbeans 7 and as my program (sometimes)

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.