Skip to content

Instantly share code, notes, and snippets.

@chaitanyav
Created April 6, 2013 14:48
Show Gist options
  • Save chaitanyav/5326340 to your computer and use it in GitHub Desktop.
Save chaitanyav/5326340 to your computer and use it in GitHub Desktop.
/*
* Author: NagaChaitanya Vellanki
*
* TRY/THROW/CATCH example
* Reference: http://www.di.unipi.it/~nids/docs/longjump_try_trow_catch.html
* use gcc -E -P exception_handling_1.c to the output of the preprocessor
*/
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#define TRY do{ jmp_buf env; if(!setjmp(env)) {
#define CATCH } else {
#define END_TRY } }while(0)
#define THROW longjmp(env, 1)
int main(int argc, char *argv[]) {
TRY {
printf("In TRY statement\n");
THROW;
printf("not reachable\n");
}
CATCH {
printf("exception caught\n");
}
END_TRY;
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment