Created
October 21, 2021 20:40
-
-
Save Silva97/726676dec7311f45329296787780c477 to your computer and use it in GitHub Desktop.
PoC macros to do error handling in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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