Exception Handling? Impossible!
richw at ada-uts.UUCP
richw at ada-uts.UUCP
Tue Aug 27 06:05:00 AEST 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 <setjmp.h>
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 <setjmp.h>
#include <stdio.h>
#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);
}
More information about the Comp.lang.c
mailing list