0

Error handling code for any general function follows this template Two questions from my end--

  1. What should be the default FAILURE value? (1 to keep up with the main or 0, or -1 to avoid all the confusion)
  2. What should be the initial value of status? (FAIL or PASS)

Code:

#define FAILURE 0  //or shall it be 1 for success and 0 for failure
#define SUCCESS 1

int DoSomething() {
    int status = FAILURE;  //or shall we assign success by default?

    if (error1)
        return FAIL_A
    if (error2)
        return FAIL_B

    return SUCCESS;
}

int GetItDone() {
   status = FAIL;

   Status = DoSomething();
   if (PASS != status) //likewise many calls can happen later
     goto END;

END:
  return status;
    }

The status bubble up through function calls.

  • Don't trust your functions, start with FAILURE - that's what I do – mark Oct 09 '15 at 06:17
  • You can't start with failure if, for example, you have to loop and look for a failing condition. The code above doesn't make sense because `status` is never actually used. – Millie Smith Oct 09 '15 at 06:21
  • There are like a dozen different ways to handle returning error values. You could perhaps provide a bit more context... Like, do you also return result value, or only success / error code? Also, try to make the code sample to make sense... now `status` is never used, so the code snippet is just confusing. – hyde Oct 09 '15 at 06:24
  • Since you're inserting the values in the return statements, the variable `status` is unused and should be deleted, not initialized. Or you need to rewrite your pseudo-code to set `status` to the different values and then `return status;` at the end. – Jonathan Leffler Oct 09 '15 at 06:31
  • Possibly interesting related question: [Should I set errno?](http://stackoverflow.com/q/9856822/1717300) – hyde Oct 09 '15 at 06:31
  • @hyde the functions are chained and the caller does a call like this. Let me edit in the question – Code_Complete Oct 09 '15 at 06:39
  • Your methodology in `GetItDone` is horrible. why use `goto` instead of `return FAIL;`? Why write the test backwards? Why use UPPERCASE labels? All these lessen code readability. – chqrlie Oct 09 '15 at 06:52
  • Just do `if (PASS == status)` and then put the rest of the function in there. – Millie Smith Oct 09 '15 at 06:58

2 Answers2

3

The convention is 0 for success, negative values for various failures, and positive values for predicated successes.

Of these, returning 0 for success is the most frequently observed: doing otherwise would be idiosyncratic.

As for your code, setting the status to a failure code initially and changing it to success as appropriate will afford more program stability.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Thanks. setting it to FAIL increases stability but the problem is that we need to explicitly assign it to SUCCESS at the end. What is more prevalent in general practice?. I am using this for C++ as well. (can't use exceptions due to some reason) – Code_Complete Oct 09 '15 at 06:20
  • The *standard library* convention is 0 for success, -1 for error with `errno` indicating the error code. I wouldn't say there is a general C convention for non-negative to mean success (not to mention, the whole `errno` mechanism is kinda horrible in a few ways), even though it is common (because often negative return value would not make sense otherwise, so it's convenient to use that for returning error). – hyde Oct 09 '15 at 06:26
1

Returning 0 for success lets you return different values in case of failure

#define SUCCESS 0
#define FAILURE_CASE_1 -1
#define FAILURE_CASE_2 -2
user4780495
  • 2,642
  • 2
  • 18
  • 24