Skip to content

Instantly share code, notes, and snippets.

@redraiment
Created August 4, 2014 05:32
Show Gist options
  • Save redraiment/f51a44866086e40a2059 to your computer and use it in GitHub Desktop.
Save redraiment/f51a44866086e40a2059 to your computer and use it in GitHub Desktop.
Implement try-catch exception in C
#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