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 * 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#include "nilorea/n_log.h"
30
31int array_exception(int boolean) {
32 if (boolean) {
33 n_log(LOG_NOTICE, "Throwed signal");
35 }
36 n_log(LOG_NOTICE, "No signal Throwed !");
37
38 return 1;
39}
40
41int divzero_exception(int boolean) {
42 if (boolean) {
43 n_log(LOG_NOTICE, "Throwed signal");
45 }
46 n_log(LOG_NOTICE, "No signal Throwed !");
47
48 return 1;
49}
50
51int overflow_exception(int boolean) {
52 if (boolean) {
53 n_log(LOG_NOTICE, "Throwed signal");
55 }
56 n_log(LOG_NOTICE, "No signal Throwed !");
57
58 return 1;
59}
60
61int parsing_exception(int boolean) {
62 if (boolean) {
63 n_log(LOG_NOTICE, "Throwed signal");
65 }
66 n_log(LOG_NOTICE, "No signal Throwed !");
67
68 return 1;
69}
70
71int all_exception(int boolean) {
72 if (boolean) {
73 n_log(LOG_NOTICE, "Throwed signal");
75 }
76 n_log(LOG_NOTICE, "No signal Throwed !");
77
78 return 1;
79}
80
81int main(void) {
84
85 puts("ArrayNoCatch");
86 Try {
87 n_log(LOG_INFO, "Trying signal:ARRAY_EXCEPTION false");
89 n_log(LOG_NOTICE, "Trying signal:ARRAY_EXCEPTION true");
91 }
92 EndTry;
93 puts("ArrayCatch");
94 Try {
95 n_log(LOG_NOTICE, "Trying signal:ARRAY_EXCEPTION false");
97 n_log(LOG_NOTICE, "Trying signal:ARRAY_EXCEPTION true");
99 }
101 n_log(LOG_NOTICE, "Caught signal ARRAY_EXCEPTION");
102 }
103 EndTry;
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 Try {
124 n_log(LOG_NOTICE, "Trying signal:OVERFLOW_EXCEPTION false");
126 n_log(LOG_NOTICE, "Trying signal:OVERFLOW_EXCEPTION true");
128 }
129 EndTry;
130 Try {
131 n_log(LOG_NOTICE, "Trying signal:OVERFLOW_EXCEPTION false");
133 n_log(LOG_NOTICE, "Trying signal:OVERFLOW_EXCEPTION true");
135 }
137 n_log(LOG_NOTICE, "Caught signal OVERFLOW_EXCEPTION ");
138 }
139 EndTry;
140 Try {
141 n_log(LOG_NOTICE, "Trying signal:PARSING_EXCEPTION false");
143 n_log(LOG_NOTICE, "Trying signal:PARSING_EXCEPTION true");
145 }
146 EndTry;
147 Try {
148 n_log(LOG_NOTICE, "Trying signal:PARSING_EXCEPTION false");
150 n_log(LOG_NOTICE, "Trying signal:PARSING_EXCEPTION true");
152 }
154 n_log(LOG_NOTICE, "Caught signal PARSING_EXCEPTION");
155 }
156 EndTry;
157 Try {
158 n_log(LOG_NOTICE, "Trying signal:ALL_EXCEPTION false");
159 all_exception(0);
160 n_log(LOG_NOTICE, "Trying signal:ALL_EXCEPTION true");
161 all_exception(1);
162 }
163 EndTry;
164 Try {
165 n_log(LOG_NOTICE, "Trying signal:ALL_EXCEPTION false");
166 all_exception(0);
167 n_log(LOG_NOTICE, "Trying signal:ALL_EXCEPTION true");
168 all_exception(1);
169 }
171 n_log(LOG_NOTICE, "Caught signal matching GENERAL_EXCEPTION");
172 }
173 EndTry;
174
175// nest Try Catch blocks are going to generate a masked (shadowed) variable declaration warning, hence the temporary disable
176#pragma GCC diagnostic push
177#pragma GCC diagnostic ignored "-Wshadow"
178 Try {
179 n_log(LOG_NOTICE, "Trying mixed Try-Catch blocks, we are inside block 1");
180 Try {
181 n_log(LOG_NOTICE, "We're inside block 2");
183 }
185 n_log(LOG_NOTICE, "Caught signal matching GENERAL_EXCEPTION inside block 2");
186 }
187 EndTry;
189 }
191 n_log(LOG_NOTICE, "Caught signal matching GENERAL_EXCEPTION inside block 1");
192 }
193 EndTry;
194#pragma GCC diagnostic pop
195
196 /* test CatchAll macro */
197 puts("CatchAll test");
198 Try {
199 n_log(LOG_NOTICE, "Trying CatchAll: throwing DIVZERO_EXCEPTION");
201 }
202 CatchAll() {
203 n_log(LOG_NOTICE, "CatchAll caught the exception");
204 }
205 EndTry;
206
207 exit(0);
208}
int main(void)
int overflow_exception(int boolean)
int array_exception(int boolean)
int all_exception(int boolean)
int divzero_exception(int boolean)
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:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_STDERR
internal, default LOG_TYPE
Definition n_log.h:50
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_INFO
informational
Definition n_log.h:82
Exception management for C.
Generic log system.