Created
August 4, 2014 05:32
-
-
Save redraiment/f51a44866086e40a2059 to your computer and use it in GitHub Desktop.
Implement try-catch exception in C
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 <stdlib.h> | |
#include <stdio.h> | |
#include <setjmp.h> | |
jmp_buf __exception_context; | |
#define try if(!setjmp(__exception_context)) | |
#define catch else | |
#define throw_exception longjmp(__exception_context, 1) | |
void foo(void) { | |
puts("in foo"); | |
throw_exception; | |
} | |
int main(int argc, char* argv[]) { | |
try { | |
puts("you can see this"); | |
foo(); | |
puts("never see this"); | |
} catch { | |
puts("catch an exception"); | |
} | |
puts("final"); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment