Error handling code for any general function follows this template Two questions from my end--
- What should be the default FAILURE value? (1 to keep up with the main or 0, or -1 to avoid all the confusion)
- 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.