/* Catch-test.m * * Tests catch, throw and unwind-protect in Objective-C */ #include #include void foo(void); void bar(void); void call( void (*fn) (void), int i); id tag; int main(int argc, char **argv) { int result; tag = [Catch new]; if ((result = set_catch([tag catch])) == 0) { printf("main ...\n"); call(foo,3); printf("main, after foo.\n"); result = 7; } #if 0 else result = (int) [tag value]; #endif printf("Back in main, result is %d\n",result); [tag cleanup]; if ((result = set_catch([tag catch])) == 0) { printf("main ...\n"); call(bar,11); printf("main, after bar.\n"); result =17; } [tag free]; printf("Back in main, result is %d\n",result); return 0; } void call (void (*fn)(void), int i) { void cleanup(void) { printf("Cleaning up. The number is %d\n", i); } id protect = [UnwindProtect new]; [protect cleanupByCalling: cleanup]; printf("Calling...\n"); fn(); [protect cleanup]; /* Freed automatically! */ printf("Call finished\n"); } void foo() { printf("foo, returning\n"); } void bar() { printf("bar, throwing...\n"); [tag throwInt: 17]; printf("Can't happen"); }