4

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;
}
5
  • 3
    You probably want separate questions for C and C++, since the error handling idioms will be very different for each. Commented Mar 6, 2013 at 9:10
  • 1
    this is a c question. c++ has better ways of error handling. the 2nd one is definitely not preferred in c. goto is synonymous with havoc. never use it. you have many ways to get around it. Commented Mar 6, 2013 at 9:11
  • I've seen both. The last time I've seen the goto error idiom it was corporate mandated coding style. Commented Mar 6, 2013 at 9:13
  • @JoachimPileborg is it not better to use flags and such than the goto statement even though this is within the scope? Commented Mar 6, 2013 at 9:19
  • 1
    @Koushik As all questions about coding styles, this is very much personal. Unless mandated by others, do what you feel most comfortable with. Commented Mar 6, 2013 at 9:21

6 Answers 6

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

3
int function_sequence(...)
{
    if (f1(...) && f2(...) && f3(...)  && f4(...) && f5(...)) 
        return SUCCESS;
    else
        return ERROR;
}

2 Comments

This complicates a bit reading and debugging, as you are not able to set breakpoint in a separate functions.
Thats true, but you can set breakpoint inside each function, don't you?
1

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

Comments

1

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() &&.... ;

Comments

1

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

1 Comment

Is this valid C code? What is the context it is pasted from? main? I just can't seem to figure out whether you then have definition of function_sequence procedure inside the body of another procedure (context) or this is more of a pseude-code with some valid C syntax.
1

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.

3 Comments

This code would still invoke the error handling even if all the calls succeed.
add a return success above the error label. It will be correct, though not good.
I change it the real way i'm doing it... ( copy / paste mistake :/)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.