Error checking on many function calls

c, c++, error-handling, recursion

Solution

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
    }
}

Problem

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!

Original source