Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: $Revision: 1.6.2.16 $; site ada-uts.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!panda!talcott!harvard!bbnccv!ada-uts!richw From: richw@ada-uts.UUCP Newsgroups: net.lang.c Subject: Re: Exception Handling? Impossible! Message-ID: <10200005@ada-uts.UUCP> Date: Mon, 26-Aug-85 16:05:00 EDT Article-I.D.: ada-uts.10200005 Posted: Mon Aug 26 16:05:00 1985 Date-Received: Sat, 31-Aug-85 03:34:09 EDT References: <10200002@ada-uts.UUCP> Lines: 67 Nf-ID: #R:ada-uts:10200002:ada-uts:10200005:000:1687 Nf-From: ada-uts!richw Aug 26 16:05:00 1985 Sorry if this is getting boring, but... I fixed a bug; a "raise" occuring before a "Begin" is encountered works as one would expect now. I also made things a little more space efficient. Otherwise, the macros' behavior hasn't changed. Also, note that the environment stack has a fixed depth -- this can be changed (at the expense of more procedure calls) by having pushed environments be allocated from the heap (i.e. use malloc). The updated exceptions.{c,h} follow: ----------------------- exceptions.h ---------------------------- #include typedef char *Exception; extern jmp_buf _env_stack[]; extern int _env_index, _bad_index; extern Exception _exc; #define Begin { if (_env_index == _bad_index) _env_overflow();\ if (!setjmp(_env_stack[_env_index++])) { #define Except _env_index--; #define When(e) } else if (_exc == e) { #define Others } else if (1) { #define End } else _unhandled(); } #define raise(e) (_exc = e,\ (_env_index == 0)\ ? _unhandled() :\ longjmp(_env_stack[--_env_index],1) ) --------------------------- exceptions.c ---------------------------- #include #include #define BELL '\007' /* ASCII control-G */ #define STACK_SIZE 101 jmp_buf _env_stack[STACK_SIZE]; int _env_index = 0; int _bad_index = STACK_SIZE; char *_exc; _unhandled() { fflush(stdout); putc(BELL, stderr); fprintf(stderr, "\nUnhandled Exception: %s\n", _exc); exit(1); } _env_overflow() { fflush(stdout); putc(BELL, stderr); fprintf(stderr, "\nException handlers nested too deeply"); fprintf(stderr, "\nMaximum is %d\n", STACK_SIZE - 1); exit(1); }