Skip to content

Instantly share code, notes, and snippets.

@Silva97
Created October 21, 2021 20:40
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 Silva97/726676dec7311f45329296787780c477 to your computer and use it in GitHub Desktop.
Save Silva97/726676dec7311f45329296787780c477 to your computer and use it in GitHub Desktop.
PoC macros to do error handling in C
#include <stdio.h>
#include <string.h>
#define CATCH(name) \
if (0) \
_catch_##name:
#define FAIL(name) goto _catch_##name
#define FAIL_IF(expr, name) \
if (expr) \
{ \
FAIL(name); \
}
int main(int argc, char **argv)
{
FAIL_IF(argc < 2, invalid_arguments);
FAIL_IF(strcmp(argv[1], "hi"), invalid_arguments);
CATCH(invalid_arguments)
{
puts("Erro: Invalid arguments! :@");
return 1;
}
puts("Hi! :)");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment