In the case of the following code structure:
int function_sequence(...)
{
f1(...);
f2(...);
f3(...);
f4(...);
f5(...);
return SUCCESS;
}
Which way of the error handling is more acceptable?
This:
int function_sequence(...)
{
if(f1(...)){
// Error handling
return ERROR;
}
if(f2(...)){
// Error handling
return ERROR;
}
...
return SUCCESS;
}
Or this:
int function_sequence(...)
{
if(f1(...)){
goto error;
}
if(f2(...)){
goto error;
}
...
return SUCCESS;
error:
// Error handling
return ERROR;
}
For C, I prefer your second option.
Further, it's useful to do gradual cleanup (free allocated memory and so on), like in Linux kernel:
int function_sequence(...)
{
if(f1(...)){
goto error1;
}
if(f2(...)){
goto error2;
}
...
return SUCCESS;
error2:
cleanup2();
error1:
cleanup1();
// Error handling
return ERROR;
}
int function_sequence(...)
{
if (f1(...) && f2(...) && f3(...) && f4(...) && f5(...))
return SUCCESS;
else
return ERROR;
}
If it's an exceptional thing that makes the function not work, throw an exception.
If you have reason to stick to a return code, you can use
if ( f1() && f2() && .... )
return SUCCESS;
else
return ERROR;
Execution will stop after the first false
or 0
is encountered, so the effects are pretty much the same as your version.
Some libraries return 0
on success (heh, even C++'s main
does so). So you might want
if ( !f1() &&.... )
or a simple
return f1() && f2() &&.... ;
This is what i normally use
int function_sequence(...)
{
if(f1(...)){
goto error1;
}
if(f2(...)){
goto error2;
}
return SUCCESS;
}
error1:
// Error 1 handling
return -eerno1;
error2:
// Error 2 handling
return -eerno2;
//end of main
The way i like to do is the following in c langage :
int function_sequence(...)
{
int res=0;
if(f1(...)){
goto ENDF;
}
if(f2(...)){
goto ENDF;
}
...
res = 1;
ENDF:
if(res ==0)
{
//Error handling
}
//Success and Error stuff to do (like free...).
return res;
}
So there is only one return, and sometimes there is some actions to do in both : error and success (such as free...).
I guess my point of view can be discussed.
In C, I'd use the third option:
int function_sequence(...)
{
bool ok = true;
if (ok) ok = f1(...);
if (ok) ok = f2(...);
if (ok) ok = f3(...);
if (ok) ok = f4(...);
if (ok) ok = f5(...);
if (!ok) error_handling();
cleanup(); // Your example didn't have this, but it's often here.
return ok ? SUCCESS : ERROR;
}