|
|
1.1 ! root 1: /* ! 2: * a test program for exception handling - cc test.c -lexcept ! 3: */ ! 4: ! 5: #include <stdio.h> ! 6: #include <except.h> ! 7: ! 8: #define EX_EOF -2 ! 9: #define EX_ZERO -3 ! 10: ! 11: main() ! 12: { ! 13: ExceptMode = EX_MODE_REPORT | EX_MODE_ABORT; ! 14: ! 15: printf("With handler\n"); ! 16: DURING ! 17: foo(3); ! 18: HANDLER ! 19: switch (Exception.Code) { ! 20: case EX_EOF: ! 21: printf("End of input file\n"); ! 22: break; ! 23: ! 24: default: ! 25: RERAISE; ! 26: break; ! 27: } ! 28: END_HANDLER; ! 29: ! 30: printf("Without handler\n"); ! 31: foo(3); ! 32: } ! 33: ! 34: foo(i) ! 35: int i; ! 36: { ! 37: char line[64]; ! 38: int x; ! 39: ! 40: while (--i) { ! 41: printf("Enter integer> "); ! 42: clearerr(stdin); ! 43: if (gets(line) == NULL) raise(EX_EOF, "End of File"); ! 44: sscanf(line,"%d", &x); ! 45: DURING ! 46: bar(x); ! 47: HANDLER ! 48: switch (Exception.Code) { ! 49: case EX_ZERO: ! 50: printf("%s\n", Exception.Message); ! 51: E_RETURN_VOID; ! 52: default: RERAISE; break; ! 53: } ! 54: END_HANDLER ! 55: } ! 56: } ! 57: ! 58: bar(x) ! 59: int x; ! 60: { ! 61: if (x == 0) raise(EX_ZERO, "Division by Zero"); ! 62: ! 63: printf("1/%d = %f\n",x,1.0/(x+0.0)); ! 64: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.