Hello All!

To check for return values from a function and setting error flags, i am currently using the following approach:

bool fine = true;
fine &= func1();
fine &= func2();
.. and so on.

if (!fine)
{
    // error 
}

Could anyone please let me know if there is a neater(better?) way to do the same?

Thanks in advance!

Recommended Answers

All 5 Replies

I wouldn't use bool for a bitset of error flags, but that approach isn't bad. In fact, iostream implementations will typically use it for the error state of a stream.

What kind of errors are these? Have you considered exceptions?

I agree... its a decent way to check the outcome of your functions... alternatively you can use a try catch... but it comes down to the same

I would suggest the try catch method.
just inserting all those function calls into one TRY statement
and each could throw a different warning class to be caught on the outside

that way instead of running all the function you could stop after the first error.
but that depends on your needs .

good luck

If each of the calls depends on the previous one rturning a true value then you can use the && operator to chain them. Something like:

if ( func1() && func2() && func3() && func4() ) {
   std::cout << "Success!" << std::endl;
} else {
   std::cout << "Error at some point in the chain" << std::endl;
}

However, this leaves you with no way to determine where the failure occurred. That may or may not present a problem for your scenario.

Hello All, thanks a lot for the suggestions!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.