Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_exceptions.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#ifndef ____EXCEPTION___MANAGEMENT__FOR_C
28#define ____EXCEPTION___MANAGEMENT__FOR_C
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
39#include <stdio.h>
40#include <stdlib.h>
41#include <setjmp.h>
42
44#define NO_EXCEPTION 0
46#define ARRAY_EXCEPTION 2
48#define DIVZERO_EXCEPTION 4
50#define OVERFLOW_EXCEPTION 8
52#define PARSING_EXCEPTION 16
53
55#define GENERAL_EXCEPTION (ARRAY_EXCEPTION | DIVZERO_EXCEPTION | OVERFLOW_EXCEPTION | PARSING_EXCEPTION)
56
64
66extern __thread ExceptionContextList* __Exceptions;
67
69void push_exception(void);
71void pop_exception(int ex);
72
74#define Try \
75 { \
76 volatile int __exc_; \
77 push_exception(); \
78 __exc_ = setjmp(__Exceptions->context); \
79 if (__exc_ == NO_EXCEPTION) {
81#define Catch(X) \
82 } \
83 if (__exc_ & (X)) { \
84 __exc_ = 0;
85
87#define CatchAll() \
88 } \
89 if (__exc_ != 0) { \
90 __exc_ = 0;
91
93#define EndTry \
94 } \
95 pop_exception(__exc_); \
96 }
97
101#define Throw(X) \
102 do { \
103 int __exc_val_ = (X); \
104 n_log(LOG_ERR, "Exception thrown: %d", __exc_val_); \
105 if (__Exceptions) { \
106 longjmp(__Exceptions->context, __exc_val_ ? __exc_val_ : 1); \
107 } else { \
108 n_log(LOG_ERR, "Throw called outside Try block!"); \
109 abort(); \
110 } \
111 } while (0)
112
117#ifdef __cplusplus
118}
119#endif
120
121#endif /* #ifndef ____EXCEPTION___MANAGEMENT__FOR_C */
jmp_buf context
saving the current context
struct ExceptionContextList * head
pointer to the next stacked exception
__thread ExceptionContextList * __Exceptions
Globale variable for exception stack management.
void pop_exception(int ex)
pop an exception context from the stack
void push_exception(void)
add an exception context to the stack
Exception stack structure.