: Sorry, no exceptions and throws and catches here...
:
: Since my program is in C, I can't use fancy C++ features to report errors to callers of the functions I use, so instead, I decided on a common return value set. Each function returns one value from this set:
:
:
: #define SINT_SUCCESS 0/*returned on successful operation*/
: #define SINT_ERR_ARG 1/*one or more of the arguments passed are faulty*/
: #define SINT_ERR_DYNMEM 2/*there was an error in dynamic allocation*/
: #define SINT_ERR_TIDY 3/*removal of excess bytes in the output failed. This is not fatal*/
: #define SINT_ERR_IO_BADBASE 4/*the base passed to an IO function is not supported*/
: ...
:
:
:
: Now there is a weakness in this system. Only
one error can be reported for each call. Lets say we have some number of errors, we need to be able to report each, right? Now I could easily use bitfields to return more error information, but, as of now though, all my functions don't have such complex error possibilities...
:
: But, do you guys think it would be good to prepare my code(mainly, making the errors multiples of 2, so they can represent individual bits), so that if one day, I might need to return complex errors, it wouldn't be too hard to change the code?
:
: Thanx...
: {2}rIng
:
The common way to do this in C is like this:
typedef enum
{
SINT_SUCCESS = 0x00,
SINT_ERR_ARG = 0x01,
SINT_ERR_DYNMEM = 0x02,
SINT_ERR_TIDY = 0x04,
SINT_ERR_IO_BADBASE = 0x08,
SINT_ANOTHER_ERROR = 0x10,
...
} ErrorType;
ErrorType myErrorProneFunction (void);
int main()
{
ErrorType result;
result = myErrorProneFunction();
if(result != SINT_SUCCESS)
{
if(result & SINT_ERR_ARG)
{
/* error handling here */
}
}
return 0;
}