I have a question related to c++ exceptions. I understand that in many cases throwing exceptions is better than returning error values. However, I do find the following situation where returning error values is more feasible then exceptions:
bool run_a_function()
{
if (!function_step1())
return false;
if (!function_step2())
return false;
if (!function_step3())
return false;
if (!function_step4())
return false;
}
As we can see from the above codes, in order to finish a function, several steps are needed. If one step fails, a false value is returned. Then when an operator use the function run_a_function
he can make sure that the program will not crash even this function does not run well:
if (run_a_function())
do_something();
else
do_other_things();
However, if we use exceptions:
void run_a_function()
{
try
{
function_step1();
function_step2();
function_step3();
function_step4();
}
catch (...)
{
}
}
then I have the risk that the program crash and an exception is threw. My question is: have I justified returning error values in this case? Are there some guild lines for choosing error values and exceptions? Thanks!