#include struct _catch { struct _catch *next; jmp_buf buf; }; static struct _catch _catch_top[1]; struct _catch *_catch = _catch_top; #define throw() \ do { \ _catch = _catch->next; \ longjmp(_catch->buf, 1); \ } while (0) #define try \ if (setjmp(_catch->buf) == 0) { \ struct _catch _catch_new[1]; \ _catch_new->next = _catch; \ _catch = _catch_new; #define catch \ _catch = _catch->next; \ } else /**** example below ****/ #include int a = 1; void foo(int x) { a=2; if (x & 1) throw(); } int main(int argc, char **argv) { int i; for (i = 0; i < 10; ++i) { a=i; try { try foo(i); catch { printf("2: caught at %d (a=%d)\n", i, a); if ((i&3)==1) throw(); } } catch { printf("1: caught at %d (a=%d)\n", i, a); } printf("a=%d\n", a); } return 0; }