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 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28#ifndef ____EXCEPTION___MANAGEMENT__FOR_C
29#define ____EXCEPTION___MANAGEMENT__FOR_C
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
40#include <stdio.h>
41#include <stdlib.h>
42#include <setjmp.h>
43
45#define NO_EXCEPTION 0
47#define ARRAY_EXCEPTION 2
49#define DIVZERO_EXCEPTION 4
51#define OVERFLOW_EXCEPTION 8
53#define PARSING_EXCEPTION 16
54
56#define GENERAL_EXCEPTION (ARRAY_EXCEPTION | DIVZERO_EXCEPTION | OVERFLOW_EXCEPTION | PARSING_EXCEPTION)
57
65
67extern __thread ExceptionContextList* __Exceptions;
68
70void push_exception(void);
72void pop_exception(int ex);
73
75#define Try \
76 { \
77 volatile int __exc_; \
78 push_exception(); \
79 __exc_ = setjmp(__Exceptions->context); \
80 if (__exc_ == NO_EXCEPTION) {
82#define Catch(X) \
83 } \
84 if (__exc_ & (X)) { \
85 __exc_ = 0;
86
88#define CatchAll() \
89 } \
90 if (__exc_ != 0) { \
91 __exc_ = 0;
92
94#define EndTry \
95 } \
96 pop_exception(__exc_); \
97 }
98
102#define Throw(X) \
103 do { \
104 int __exc_val_ = (X); \
105 n_log(LOG_ERR, "Exception thrown: %d", __exc_val_); \
106 if (__Exceptions) { \
107 longjmp(__Exceptions->context, __exc_val_ ? __exc_val_ : 1); \
108 } else { \
109 n_log(LOG_ERR, "Throw called outside Try block!"); \
110 abort(); \
111 } \
112 } while (0)
113
118#ifdef __cplusplus
119}
120#endif
121
122#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.