6 Module EXCEPTIONS
gullradriel edited this page 2026-10-29 20:54:40 +01:00

EXCEPTIONS: C++ style try,catch,endtry,throw

Structures

Name
struct ExceptionContextList
Exception stack structure.

Functions

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

Attributes

Name
__thread ExceptionContextList * __Exceptions
Globale variable for exception stack management.

Defines

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

Functions Documentation

function push_exception

void push_exception(
    void 
)

add an exception context to the stack

Function to push an exception in the list.

add an exception context to the stack

function pop_exception

void pop_exception(
    int ex
)

pop an exception context from the stack

Parameters:

  • ex Type of exception to pop

Function to pop an exception in the list.

pop an exception context from the stack

Attributes Documentation

variable __Exceptions

__thread ExceptionContextList * __Exceptions;

Globale variable for exception stack management.

Macros Documentation

define Try

#define Try {                                           \
    volatile int __exc_;                    \
    push_exception();                       \
    __exc_ = setjmp(__Exceptions->context); \
    if (__exc_ == NO_EXCEPTION) {

Macro for replacing try.

define Throw

#define Throw(
    X
)
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)

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.

define PARSING_EXCEPTION

#define PARSING_EXCEPTION 16

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

define OVERFLOW_EXCEPTION

#define OVERFLOW_EXCEPTION 8

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

define NO_EXCEPTION

#define NO_EXCEPTION 0

Possibly Throwed value, everything is fine.

define GENERAL_EXCEPTION

#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 EndTry

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

Macro helper for closing the try-catch block.

define DIVZERO_EXCEPTION

#define DIVZERO_EXCEPTION 4

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

define CatchAll

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

Macro for replacing catch.

define Catch

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

Macro for replacing catch.

define ARRAY_EXCEPTION

#define ARRAY_EXCEPTION 2

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