Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_exceptions.c
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#include "nilorea/n_log.h"
29
30int array_exception(int boolean) {
31 if (boolean) {
32 n_log(LOG_NOTICE, "Throwed signal");
34 }
35 n_log(LOG_NOTICE, "No signal Throwed !");
36
37 return 1;
38}
39
40int divzero_exception(int boolean) {
41 if (boolean) {
42 n_log(LOG_NOTICE, "Throwed signal");
44 }
45 n_log(LOG_NOTICE, "No signal Throwed !");
46
47 return 1;
48}
49
50int overflow_exception(int boolean) {
51 if (boolean) {
52 n_log(LOG_NOTICE, "Throwed signal");
54 }
55 n_log(LOG_NOTICE, "No signal Throwed !");
56
57 return 1;
58}
59
60int parsing_exception(int boolean) {
61 if (boolean) {
62 n_log(LOG_NOTICE, "Throwed signal");
64 }
65 n_log(LOG_NOTICE, "No signal Throwed !");
66
67 return 1;
68}
69
70int all_exception(int boolean) {
71 if (boolean) {
72 n_log(LOG_NOTICE, "Throwed signal");
74 }
75 n_log(LOG_NOTICE, "No signal Throwed !");
76
77 return 1;
78}
79
80int main(void) {
83
84 puts("ArrayNoCatch");
85 Try {
86 n_log(LOG_INFO, "Trying signal:ARRAY_EXCEPTION false");
88 n_log(LOG_NOTICE, "Trying signal:ARRAY_EXCEPTION true");
90 }
91 EndTry;
92 puts("ArrayCatch");
93 Try {
94 n_log(LOG_NOTICE, "Trying signal:ARRAY_EXCEPTION false");
96 n_log(LOG_NOTICE, "Trying signal:ARRAY_EXCEPTION true");
98 }
100 n_log(LOG_NOTICE, "Caught signal ARRAY_EXCEPTION");
101 }
102 EndTry;
103 /***************************************************************/
104 puts("DivZeroNoCatch");
105 Try {
106 n_log(LOG_NOTICE, "Trying signal:DIVZERO_EXCEPTION false");
108 n_log(LOG_NOTICE, "Trying signal:DIVZERO_EXCEPTION true");
110 }
111 EndTry;
112 puts("DivZeroCatch");
113 Try {
114 n_log(LOG_NOTICE, "Trying signal:DIVZERO_EXCEPTION false");
116 n_log(LOG_NOTICE, "Trying signal:DIVZERO_EXCEPTION true");
118 }
120 n_log(LOG_NOTICE, "Caught signal DIVZERO_EXCEPTION");
121 }
122 EndTry;
123 /***************************************************************/
124 Try {
125 n_log(LOG_NOTICE, "Trying signal:OVERFLOW_EXCEPTION false");
127 n_log(LOG_NOTICE, "Trying signal:OVERFLOW_EXCEPTION true");
129 }
130 EndTry;
131 Try {
132 n_log(LOG_NOTICE, "Trying signal:OVERFLOW_EXCEPTION false");
134 n_log(LOG_NOTICE, "Trying signal:OVERFLOW_EXCEPTION true");
136 }
138 n_log(LOG_NOTICE, "Caught signal OVERFLOW_EXCEPTION ");
139 }
140 EndTry;
141 /***************************************************************/
142 Try {
143 n_log(LOG_NOTICE, "Trying signal:PARSING_EXCEPTION false");
145 n_log(LOG_NOTICE, "Trying signal:PARSING_EXCEPTION true");
147 }
148 EndTry;
149 Try {
150 n_log(LOG_NOTICE, "Trying signal:PARSING_EXCEPTION false");
152 n_log(LOG_NOTICE, "Trying signal:PARSING_EXCEPTION true");
154 }
156 n_log(LOG_NOTICE, "Caught signal PARSING_EXCEPTION");
157 }
158 EndTry;
159 /***************************************************************/
160 Try {
161 n_log(LOG_NOTICE, "Trying signal:ALL_EXCEPTION false");
162 all_exception(0);
163 n_log(LOG_NOTICE, "Trying signal:ALL_EXCEPTION true");
164 all_exception(1);
165 }
166 EndTry;
167 Try {
168 n_log(LOG_NOTICE, "Trying signal:ALL_EXCEPTION false");
169 all_exception(0);
170 n_log(LOG_NOTICE, "Trying signal:ALL_EXCEPTION true");
171 all_exception(1);
172 }
174 n_log(LOG_NOTICE, "Caught signal matching GENERAL_EXCEPTION");
175 }
176 EndTry;
177
178// nest Try Catch blocks are going to generate a masked (shadowed) variable declaration warning, hence the temporary disable
179#pragma GCC diagnostic push
180#pragma GCC diagnostic ignored "-Wshadow"
181 Try {
182 n_log(LOG_NOTICE, "Trying mixed Try-Catch blocks, we are inside block 1");
183 Try {
184 n_log(LOG_NOTICE, "We're inside block 2");
186 }
188 n_log(LOG_NOTICE, "Caught signal matching GENERAL_EXCEPTION inside block 2");
189 }
190 EndTry;
192 }
194 n_log(LOG_NOTICE, "Caught signal matching GENERAL_EXCEPTION inside block 1");
195 }
196 EndTry;
197#pragma GCC diagnostic pop
198
199 /* test CatchAll macro */
200 puts("CatchAll test");
201 Try {
202 n_log(LOG_NOTICE, "Trying CatchAll: throwing DIVZERO_EXCEPTION");
204 }
205 CatchAll() {
206 n_log(LOG_NOTICE, "CatchAll caught the exception");
207 }
208 EndTry;
209
210 exit(0);
211}
int overflow_exception(int boolean)
int array_exception(int boolean)
int all_exception(int boolean)
int divzero_exception(int boolean)
int main(void)
int parsing_exception(int boolean)
#define Throw(X)
Macro helper for adding exception throwing in custom functions.
#define OVERFLOW_EXCEPTION
Possibly Throwed value, we checked an overflow in our arrays.
#define Try
Macro for replacing try.
#define GENERAL_EXCEPTION
General exception, we just detected an error an decided to go back where we're safe.
#define CatchAll()
Macro for replacing catch.
#define PARSING_EXCEPTION
Possibly Throwed value, we checked an error during a char * parsing.
#define ARRAY_EXCEPTION
Possibly Throwed value, we checked an out of bound operation.
#define Catch(X)
Macro for replacing catch.
#define DIVZERO_EXCEPTION
Possibly Throwed value, we check a divide by zero operation.
#define EndTry
Macro helper for closing the try-catch block.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_STDERR
internal, default LOG_TYPE
Definition n_log.h:49
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_INFO
informational
Definition n_log.h:81
Exception management for C.
Generic log system.