Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_exceptions.c

Nilorea Library exception api test.

Nilorea Library exception api test

Author
Castagnier Mickael
Version
1.0
Date
24/10/2018
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "nilorea/n_log.h"
int array_exception(int boolean) {
if (boolean) {
n_log(LOG_NOTICE, "Throwed signal");
}
n_log(LOG_NOTICE, "No signal Throwed !");
return 1;
}
int divzero_exception(int boolean) {
if (boolean) {
n_log(LOG_NOTICE, "Throwed signal");
}
n_log(LOG_NOTICE, "No signal Throwed !");
return 1;
}
int overflow_exception(int boolean) {
if (boolean) {
n_log(LOG_NOTICE, "Throwed signal");
}
n_log(LOG_NOTICE, "No signal Throwed !");
return 1;
}
int parsing_exception(int boolean) {
if (boolean) {
n_log(LOG_NOTICE, "Throwed signal");
}
n_log(LOG_NOTICE, "No signal Throwed !");
return 1;
}
int all_exception(int boolean) {
if (boolean) {
n_log(LOG_NOTICE, "Throwed signal");
}
n_log(LOG_NOTICE, "No signal Throwed !");
return 1;
}
int main(void) {
puts("ArrayNoCatch");
Try {
n_log(LOG_INFO, "Trying signal:ARRAY_EXCEPTION false");
n_log(LOG_NOTICE, "Trying signal:ARRAY_EXCEPTION true");
}
puts("ArrayCatch");
Try {
n_log(LOG_NOTICE, "Trying signal:ARRAY_EXCEPTION false");
n_log(LOG_NOTICE, "Trying signal:ARRAY_EXCEPTION true");
}
n_log(LOG_NOTICE, "Caught signal ARRAY_EXCEPTION");
}
puts("DivZeroNoCatch");
Try {
n_log(LOG_NOTICE, "Trying signal:DIVZERO_EXCEPTION false");
n_log(LOG_NOTICE, "Trying signal:DIVZERO_EXCEPTION true");
}
puts("DivZeroCatch");
Try {
n_log(LOG_NOTICE, "Trying signal:DIVZERO_EXCEPTION false");
n_log(LOG_NOTICE, "Trying signal:DIVZERO_EXCEPTION true");
}
n_log(LOG_NOTICE, "Caught signal DIVZERO_EXCEPTION");
}
Try {
n_log(LOG_NOTICE, "Trying signal:OVERFLOW_EXCEPTION false");
n_log(LOG_NOTICE, "Trying signal:OVERFLOW_EXCEPTION true");
}
Try {
n_log(LOG_NOTICE, "Trying signal:OVERFLOW_EXCEPTION false");
n_log(LOG_NOTICE, "Trying signal:OVERFLOW_EXCEPTION true");
}
n_log(LOG_NOTICE, "Caught signal OVERFLOW_EXCEPTION ");
}
Try {
n_log(LOG_NOTICE, "Trying signal:PARSING_EXCEPTION false");
n_log(LOG_NOTICE, "Trying signal:PARSING_EXCEPTION true");
}
Try {
n_log(LOG_NOTICE, "Trying signal:PARSING_EXCEPTION false");
n_log(LOG_NOTICE, "Trying signal:PARSING_EXCEPTION true");
}
n_log(LOG_NOTICE, "Caught signal PARSING_EXCEPTION");
}
Try {
n_log(LOG_NOTICE, "Trying signal:ALL_EXCEPTION false");
n_log(LOG_NOTICE, "Trying signal:ALL_EXCEPTION true");
}
Try {
n_log(LOG_NOTICE, "Trying signal:ALL_EXCEPTION false");
n_log(LOG_NOTICE, "Trying signal:ALL_EXCEPTION true");
}
n_log(LOG_NOTICE, "Caught signal matching GENERAL_EXCEPTION");
}
// nest Try Catch blocks are going to generate a masked (shadowed) variable declaration warning, hence the temporary disable
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
Try {
n_log(LOG_NOTICE, "Trying mixed Try-Catch blocks, we are inside block 1");
Try {
n_log(LOG_NOTICE, "We're inside block 2");
}
n_log(LOG_NOTICE, "Caught signal matching GENERAL_EXCEPTION inside block 2");
}
}
n_log(LOG_NOTICE, "Caught signal matching GENERAL_EXCEPTION inside block 1");
}
#pragma GCC diagnostic pop
/* test CatchAll macro */
puts("CatchAll test");
Try {
n_log(LOG_NOTICE, "Trying CatchAll: throwing DIVZERO_EXCEPTION");
}
n_log(LOG_NOTICE, "CatchAll caught the exception");
}
exit(0);
}
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.