Skip to content

Instantly share code, notes, and snippets.

@autumnharmony
Created September 24, 2011 12:08
Show Gist options
  • Save autumnharmony/1239254 to your computer and use it in GitHub Desktop.
Save autumnharmony/1239254 to your computer and use it in GitHub Desktop.
Exceptions in C with Longjmp and Setjmp
#include <stdio.h>
#include <setjmp.h>
#define TRY do{ jmp_buf ex_buf__; if( !setjmp(ex_buf__) ){
#define CATCH } else {
#define ETRY } }while(0)
#define THROW longjmp(ex_buf__, 1)
int
main(int argc, char** argv)
{
TRY
{
printf("In Try Statement\n");
THROW;
printf("I do not appear\n");
}
CATCH
{
printf("Got Exception!\n");
}
ETRY;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment