Created
August 3, 2019 13:10
-
-
Save vbogretsov/894446464495e8ccafb3b5788e6d397d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <stdarg.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| __thread static char* __error = NULL; | |
| #define raize(fmt, ...) \ | |
| do { \ | |
| size_t errlen = snprintf(NULL, 0, (fmt), ##__VA_ARGS__); \ | |
| __error = realloc(__error, errlen); \ | |
| sprintf(__error, (fmt), ##__VA_ARGS__); \ | |
| } while (0) | |
| #define fcatch(fd, fmt) \ | |
| do { \ | |
| fprintf((fd), (fmt), __error); \ | |
| catch (); \ | |
| } while (0) | |
| static inline void catch () { | |
| free(__error); | |
| __error = NULL; | |
| } | |
| #define error (__error != NULL) | |
| int test(int a) { | |
| if (a > 10) { | |
| raize("invalid ergument: %d", a); | |
| return -1; | |
| } | |
| return a * a; | |
| } | |
| int main(int argc, const char* argv[]) { | |
| int x = test(11); | |
| if (error) { | |
| fcatch(stderr, "error: %s\n"); | |
| return 1; | |
| } | |
| printf("%s\n", "ok"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment