I've been doing some reading on error handling, and I tend to use something like this at work when I'm working with various pieces of equipment, sockets, etc.
I would think this is probably outdated, and my approach could be improved quite a bit.
Should I start using inline calls. We use C99 at work, but I would like to move to C11 in my next project. They usually allow me lots of liberties in what I do as long the customer is happy with the end product.
Code:
#include <stdlib.h>#include <stdio.h>
#define ErrChk(func) error = func; if(error != SIMPLE_SUCCESS) {goto Error;}
enum SimpleErrorCases
{
SIMPLE_SUCCESS,
SIMPLE_FAILURE
};
int myErrorTest(void);
int getHandle(void);
int getHandle()
{
// simulate and error
return SIMPLE_FAILURE;
}
int myErrorTest()
{
int error;
ErrChk(getHandle());
Error:
if(error != SIMPLE_SUCCESS)
printf("There was an error.\n");
return error;
}
int main()
{
int error = 0;
myErrorTest();
system("pause");
return 0;
}