David Molnar
David Molnar

Reputation: 479

C error-handling exception like

I would like to do some error handling in C. I like C# exceptions so decided to do something like that. The goals are:

What do you think is there any problem with this approach?

The caller function:

EError activate_add(uint32_t key)
{
    if (activate_bufferSize+1>=activate_bufferMax) THROW_ERROR("activate list full");
    activate_buffer[activate_bufferSize].Id = activate_bufferSize+1;
    activate_buffer[activate_bufferSize].StoredKey = key;
    activate_bufferSize++;
    TRY(activate_saveToEEProm());
    NO_ERROR();
}

My solution is the following:

#ifndef ERROR_H_
#define ERROR_H_


typedef enum
{
    Message_None,
    Message_Error,
    Message_Warning,
    Message_Info
}EMessage_type;

typedef struct
{
    EMessage_type message_type;
    char* message;
    const char* file;
    uint32_t line;
}EErrorStruct,*EError;

#define THROW_ERROR(message, ...) { EError __err=GET_MESSAGE(Message_Error,(char*)__FILE__,__LINE__,message,##__VA_ARGS__); SEND_MESSAGE(__err); return __err;}
#define TRY(__x) { EError __err = __x; if (__err->message_type==Message_Error) { return __err;}}

#define TRYHAL(__x) { HAL_StatusTypeDef __res = __x; if (__res != HAL_OK) { THROW_ERROR("HAL problem: %u",__res);}}
#define TRYFAT(__x) { FRESULT __res = __x; if (__res != FR_OK) { THROW_ERROR("FAT problem: %u",__res);}}

#define NO_ERROR() { return GET_MESSAGE(Message_None,NULL,0,NULL,0);}
#define SEND_ERROR(message , ...) { SEND_MESSAGE(GET_MESSAGE(Message_Error,(char*)__FILE__,__LINE__,message,##__VA_ARGS__)); }
#define SEND_WARN(message , ...) { SEND_MESSAGE(GET_MESSAGE(Message_Warning,(char*)__FILE__,__LINE__,message,##__VA_ARGS__)); }
#define SEND_INFO(message , ...) { SEND_MESSAGE(GET_MESSAGE(Message_Info,(char*)__FILE__,__LINE__,message,##__VA_ARGS__)); }

EError GET_MESSAGE(EMessage_type messagetype,const char* file, uint32_t line,const char *format, ... );
void SEND_MESSAGE(EError err);
uint8_t ISFAILED(EError err);


#endif /* ERROR_H_ */

The source file

#include <error.h>
#include "stdio.h"
#include "stdarg.h"

static EErrorStruct error;
static const EError m = &error;
static char buffer[300];

EError GET_MESSAGE(EMessage_type messagetype,const char* file, uint32_t line,const char *format, ... )
{

    va_list args;
    va_start(args,format);
    vsprintf(buffer, format, args);
    va_end(args);

    m->message = buffer;
    m->message_type = messagetype;
    m->file = file;
    m->line = line;

    return m;
}



void SEND_MESSAGE(EError err)
{
    switch (err->message_type) {
        case Message_Error:
            printf("ERRO: %s \r\n\t\t\t\t\t\t\t\t\t\t at %s line: %u\r\n",err->message,err->file,(unsigned int)err->line);
            break;
        case Message_Warning:
            printf("WARN: %s \r\n\t\t\t\t\t\t\t\t\t\t at %s line: %u\r\n",err->message,err->file,(unsigned int)err->line);
            break;
        case Message_Info:
            printf("INFO: %s \r\n\t\t\t\t\t\t\t\t\t\t at %s line: %u\r\n",err->message,err->file,(unsigned int)err->line);
            break;
        default:
            break;
    }

}
uint8_t ISFAILED(EError err)
{
    if (err->message_type == Message_Error) return 1;


return 0;
}

Upvotes: 0

Views: 1194

Answers (1)

Lundin
Lundin

Reputation: 213276

The following are all widely considered as bad practice:

  • Function-like macros.
  • Variadic macros.
  • Variadic functions.
  • Inventing your own language features through macros.
  • Using non-standard language features such as ##__VA_ARGS__.

There are many reasons why these are bad practice: non-existent type safety, very hard to read, hard to debug/maintain, portability and so on.

Thus combining all of the above into a single program is a very bad idea.


Error handing in C is handled through a de facto industry standard, which basically goes like this:

  • Error codes are defined through enums.
  • The return value of any API function is reserved for the error code.
  • Upon error the function returns an error code.
  • Compilers are set to a warning level where you get a warning if you ignore the result of a function.
  • Source code documentation states what will/will not happen with other parameters upon success and error.

This is what other C programmers expect and immediately understand. They do not understand or expect some home-brewed exception handling system.

In addition, there is a lot of criticism against exception handling in languages that do support it. See Why is exception handling bad?

Upvotes: 3

Related Questions