-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexcept.h
53 lines (39 loc) · 1.13 KB
/
except.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef _EXCEPT_H_
#define _EXCEPT_H_
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Error handling magic */
typedef int ExceptionType;
struct _exception {
const char * file;
int line;
const char * function;
ExceptionType type;
const char * msg;
};
typedef struct _exception Exception;
#define _EXCEPTION_LIST_SIZE 16
/* Protected variables */
extern jmp_buf _exception_env;
extern Exception _exception_list[_EXCEPTION_LIST_SIZE];
extern int _exception_stack;
/* Syntactic sugar! Yum! */
#define _try \
if (!setjmp(_exception_env))
#define _catch(_e) \
Exception *_e = NULL; \
if (_exception_stack) { \
_e = &_exception_list[_exception_stack--]; \
} \
if (_e)
#define _throw(_e) \
if (_exception_stack >= (_EXCEPTION_LIST_SIZE - 1)) { \
fprintf(stderr, "FATAL: TOO MANY NESTED EXCEPTIONS\n"); \
abort(); \
} \
memcpy(&_exception_list[++_exception_stack], &_e, sizeof(Exception)); \
longjmp(_exception_env, _exception_stack);
#define EXCEPTION_INFO __FILE__, __LINE__, __FUNCTION__
#endif /* _EXCEPT_H_ */