Skip to content

Instantly share code, notes, and snippets.

@chaitanyav
Created April 6, 2013 14:51
Show Gist options
  • Save chaitanyav/5326356 to your computer and use it in GitHub Desktop.
Save chaitanyav/5326356 to your computer and use it in GitHub Desktop.
/*
* Author: NagaChaitanya Vellanki
*
* TRY/THROW/CATCH/FINALLY - example
* Reference: http://www.di.unipi.it/~nids/docs/longjump_try_trow_catch.html
*
* use gcc -E -P exception_handling_3.c to see the result of the preprocessing
* step
*/
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#define FOO_EXCEPTION (1)
#define BAR_EXCEPTION (2)
#define GOO_EXCEPTION (3)
#define TRY do{ jmp_buf env; switch(setjmp(env)) { case 0: while(1) {
#define CATCH(exception) break; case exception:
#define FINALLY break; } default:
#define END_TRY } }while(0)
#define THROW(exception) longjmp(env, exception)
int main(int argc, char *argv[]) {
TRY {
printf("In TRY statement\n");
THROW(GOO_EXCEPTION);
printf("not reachable\n");
}
CATCH(FOO_EXCEPTION) {
printf("FOO exception caught\n");
}
CATCH(BAR_EXCEPTION) {
printf("BAR exception caught\n");
}
CATCH(GOO_EXCEPTION) {
printf("GOO exception caught\n");
}
FINALLY {
printf("Finally \n");
}
END_TRY;
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment