Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
EXCEPTIONS: C++ style try,catch,endtry,throw

Data Structures

struct  ExceptionContextList
 Exception stack structure. More...
 

Macros

#define ARRAY_EXCEPTION   2
 Possibly Throwed value, we checked an out of bound operation.
 
#define Catch(X)
 Macro for replacing catch.
 
#define CatchAll()
 Macro for replacing catch.
 
#define DIVZERO_EXCEPTION   4
 Possibly Throwed value, we check a divide by zero operation.
 
#define EndTry
 Macro helper for closing the try-catch block.
 
#define GENERAL_EXCEPTION   (ARRAY_EXCEPTION | DIVZERO_EXCEPTION | OVERFLOW_EXCEPTION | PARSING_EXCEPTION)
 General exception, we just detected an error an decided to go back where we're safe.
 
#define NO_EXCEPTION   0
 Possibly Throwed value, everything is fine.
 
#define OVERFLOW_EXCEPTION   8
 Possibly Throwed value, we checked an overflow in our arrays.
 
#define PARSING_EXCEPTION   16
 Possibly Throwed value, we checked an error during a char * parsing.
 
#define Throw(X)
 Macro helper for adding exception throwing in custom functions.
 
#define Try
 Macro for replacing try.
 

Functions

void pop_exception (int ex)
 pop an exception context from the stack
 
void push_exception (void)
 add an exception context to the stack
 

Variables

__thread ExceptionContextList__Exceptions
 Globale variable for exception stack management.
 

Detailed Description


Data Structure Documentation

◆ ExceptionContextList

struct ExceptionContextList

Exception stack structure.

Definition at line 58 of file n_exceptions.h.

+ Collaboration diagram for ExceptionContextList:
Data Fields
jmp_buf context saving the current context
struct ExceptionContextList * head pointer to the next stacked exception

Macro Definition Documentation

◆ ARRAY_EXCEPTION

#define ARRAY_EXCEPTION   2

Possibly Throwed value, we checked an out of bound operation.

Examples
ex_exceptions.c.

Definition at line 46 of file n_exceptions.h.

◆ Catch

#define Catch (   X)
Value:
} \
if (__exc_ & (X)) { \
__exc_ = 0;

Macro for replacing catch.

Examples
ex_exceptions.c.

Definition at line 81 of file n_exceptions.h.

◆ CatchAll

#define CatchAll ( )
Value:
} \
if (__exc_ != 0) { \
__exc_ = 0;

Macro for replacing catch.

Examples
ex_exceptions.c.

Definition at line 87 of file n_exceptions.h.

◆ DIVZERO_EXCEPTION

#define DIVZERO_EXCEPTION   4

Possibly Throwed value, we check a divide by zero operation.

Examples
ex_exceptions.c.

Definition at line 48 of file n_exceptions.h.

◆ EndTry

#define EndTry
Value:
} \
pop_exception(__exc_); \
}

Macro helper for closing the try-catch block.

Examples
ex_exceptions.c.

Definition at line 93 of file n_exceptions.h.

◆ GENERAL_EXCEPTION

General exception, we just detected an error an decided to go back where we're safe.

Examples
ex_exceptions.c.

Definition at line 55 of file n_exceptions.h.

◆ NO_EXCEPTION

#define NO_EXCEPTION   0

Possibly Throwed value, everything is fine.

Definition at line 44 of file n_exceptions.h.

◆ OVERFLOW_EXCEPTION

#define OVERFLOW_EXCEPTION   8

Possibly Throwed value, we checked an overflow in our arrays.

Examples
ex_exceptions.c.

Definition at line 50 of file n_exceptions.h.

◆ PARSING_EXCEPTION

#define PARSING_EXCEPTION   16

Possibly Throwed value, we checked an error during a char * parsing.

Examples
ex_exceptions.c.

Definition at line 52 of file n_exceptions.h.

◆ Throw

#define Throw (   X)
Value:
do { \
int __exc_val_ = (X); \
n_log(LOG_ERR, "Exception thrown: %d", __exc_val_); \
if (__Exceptions) { \
longjmp(__Exceptions->context, __exc_val_ ? __exc_val_ : 1); \
} else { \
n_log(LOG_ERR, "Throw called outside Try block!"); \
abort(); \
} \
} while (0)
jmp_buf context
saving the current context
__thread ExceptionContextList * __Exceptions
Globale variable for exception stack management.
#define LOG_ERR
error conditions
Definition n_log.h:75

Macro helper for adding exception throwing in custom functions.

Note: longjmp with value 0 is replaced with 1 by the C standard, so Throw(NO_EXCEPTION) would actually trigger a catch. Avoid it.

Examples
ex_exceptions.c.

Definition at line 101 of file n_exceptions.h.

◆ Try

#define Try
Value:
{ \
volatile int __exc_; \
push_exception(); \
__exc_ = setjmp(__Exceptions->context); \
if (__exc_ == NO_EXCEPTION) {
#define NO_EXCEPTION
Possibly Throwed value, everything is fine.

Macro for replacing try.

Examples
ex_exceptions.c.

Definition at line 74 of file n_exceptions.h.

Function Documentation

◆ pop_exception()

void pop_exception ( int  ex)

pop an exception context from the stack

pop an exception context from the stack

Parameters
exType of exception to pop

Definition at line 52 of file n_exceptions.c.

References __Exceptions, ExceptionContextList::context, Free, ExceptionContextList::head, and NO_EXCEPTION.

◆ push_exception()

void push_exception ( void  )

add an exception context to the stack

add an exception context to the stack

Definition at line 37 of file n_exceptions.c.

References __Exceptions, ExceptionContextList::head, LOG_ERR, and n_log.

Variable Documentation

◆ __Exceptions

__thread ExceptionContextList* __Exceptions
extern

Globale variable for exception stack management.

Definition at line 32 of file n_exceptions.c.

Referenced by pop_exception(), and push_exception().