Skip to content

Instantly share code, notes, and snippets.

@hortinstein
Created October 11, 2020 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hortinstein/78bdb15833bfe3495a0760295f79f2c2 to your computer and use it in GitHub Desktop.
Save hortinstein/78bdb15833bfe3495a0760295f79f2c2 to your computer and use it in GitHub Desktop.

Add this toy example I wrote somewhere for error handling

#include <stdio.h>

#define FAILURE 1
#define SUCCESS 0

#define DEBUG_ERROR(fmt, args...) fprintf(stderr, "ERROR: %s:%d:%s(): " fmt "\n", \
    __FILE__, __LINE__, __func__, ##args)

#define CHECK_ARGS(a,b) \
       ({ \
        if(a){ \
           DEBUG_ERROR(b); \
           goto fail;\
        }})

#define CHECK_SUCCESS(a,b) \
       ({ \
        if(FAILURE == a){ \
           DEBUG_ERROR(b); \
           goto fail;\
        }})


int check(int *a,int *b)
{
    CHECK_ARGS((!a || !b),"argument error");

    return SUCCESS;
fail:
    return FAILURE;
}


int main(void){

    int a=4;
    int b=5; 
    CHECK_SUCCESS(check(NULL,NULL),"check1 failed");
    CHECK_SUCCESS(check(&a,&b),"check2 failed");

    return SUCCESS;
fail:
    return FAILURE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment