exceptions4c-lite 1.0
Lightweight exception handling for C
|
Really lightweight exception handling for C. More...
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
Data Structures | |
struct | e4c_exception |
Represents a specific occurrence of an exceptional situation in a program. More... | |
Macros | |
#define | EXCEPTIONS4C_LITE 1 |
Returns the major version number of this library. | |
#define | EXCEPTIONS4C_MAX_BLOCKS 32 |
Determines the maximum number of TRY blocks that can be nested. | |
#define | EXCEPTIONS4C_MAX_LENGTH 256 |
Determines the maximum length of an exception message, in characters. | |
#define | EXCEPTIONS4C_PANIC |
Determines what needs to be done in the event of too many nested TRY blocks. | |
#define | EXCEPTIONS4C_TERMINATE |
Determines what needs to be done in the event of an uncaught exception. | |
#define | TRY |
Introduces a block of code that may throw exceptions during execution. | |
#define | CATCH(exception_type) |
Introduces a block of code that handles exceptions thrown by a preceding TRY block. | |
#define | CATCH_ALL |
Introduces a block of code that handles any exception thrown by a preceding TRY block, regardless of its type. | |
#define | FINALLY |
Introduces a block of code that is executed after a TRY block, regardless of whether an exception was thrown or not. | |
#define | THROW(exception_type, error_message) |
Throws an exception, interrupting the normal flow of execution. | |
#define | THROWF(exception_type, format, ...) |
Throws an exception with a formatted error message. | |
#define | EXCEPTION |
Retrieves the last exception that was thrown. | |
#define | EXCEPTION_IS_UNCAUGHT |
Determines whether the thrown exception was not caught. | |
#define | EXCEPTION_PRINT |
Prints the current exception to standard error output and flushes it. | |
#define | EXCEPTION_RETHROW |
Throws the current exception again. | |
Typedefs | |
typedef const char * | e4c_exception_type |
Represents a category of problematic situations in a program. | |
Variables | |
struct e4c_context | exceptions4c |
Contains the current status of exceptions. | |
Really lightweight exception handling for C.
This library consists of one header file only. All you need to do is copy exceptions4c-lite.h
into your project, include it, and define a global variable exceptions4c
.
Since it's a header-only library, there is no library code to link against.
#define EXCEPTIONS4C_LITE 1 |
Returns the major version number of this library.
#define EXCEPTIONS4C_MAX_BLOCKS 32 |
Determines the maximum number of TRY blocks that can be nested.
The total number of blocks are preallocated inside the global variable that contains the current status of exceptions.
#define EXCEPTIONS4C_MAX_LENGTH 256 |
Determines the maximum length of an exception message, in characters.
The total number of characters are preallocated inside each exception object.
#define EXCEPTIONS4C_PANIC |
Determines what needs to be done in the event of too many nested TRY blocks.
#define EXCEPTIONS4C_TERMINATE |
Determines what needs to be done in the event of an uncaught exception.
#define TRY |
Introduces a block of code that may throw exceptions during execution.
The TRY block is used to define a section of code where exceptions MAY occur. It allows you to handle exceptions gracefully using other blocks that follow it. If an exception occurs, control is transferred to the appropriate block.
A single TRY block MAY be followed by:
goto
, break
, continue
, or return
.volatile
because these blocks invoke setjump
and their values would be indeterminate if they had been changed since the invocation.Example:
#define CATCH | ( | exception_type | ) |
Introduces a block of code that handles exceptions thrown by a preceding TRY block.
Use this macro to to handle a specific type of exceptions when they occur.
If type
is equal to the type of the thrown exception, then this block will be used to handle it.
One or more CATCH blocks MAY follow a TRY block. If type
doesn't match the thrown exception, then this block will be ignored, and the exception MAY be caught by the following CATCH or CATCH_ALL blocks.
goto
, break
, continue
, or return
.exception_type | The type of exception to catch. |
Example:
#define CATCH_ALL |
Introduces a block of code that handles any exception thrown by a preceding TRY block, regardless of its type.
The CATCH_ALL block works like a general CATCH block that does not require specifying the type of exception to handle. It MAY be used as a fallback for catching all exceptions, including those not explicitly declared in other CATCH blocks.
Only one CATCH_ALL block is allowed per TRY block, and it MUST appear after all type-specific CATCH blocks if any are present.
goto
, break
, continue
, or return
.Example:
#define FINALLY |
Introduces a block of code that is executed after a TRY block, regardless of whether an exception was thrown or not.
Use this macro to run a block, no matter whether an exception happens or not.
A FINALLY block MUST be preceded by a single TRY block, after all the accompanying CATCH and CATCH_ALL blocks (if any). Only one FINALLY block is allowed per block.
goto
, break
, continue
, or return
.Example:
#define THROW | ( | exception_type, | |
error_message ) |
Throws an exception, interrupting the normal flow of execution.
THROW is used within a TRY block, a CATCH block, or any other function to signal that an error has occurred. The thrown exception will be of the specified type
, and it MAY be handled by a preceding CATCH block.
If a thrown exception is not handled by any of the CATCH blocks in the current function, it propagates up the call stack to the function that called the current function. This continues until the exception is either handled by a CATCH block higher in the stack, or it reaches the top level of the program. If no CATCH block handles the exception, the program terminates and an error message is printed to the console.
Example:
exception_type | The type of the exception to throw. |
error_message | The error message. |
#define THROWF | ( | exception_type, | |
format, | |||
... ) |
Throws an exception with a formatted error message.
This macro works just like THROW, but it allows you to format the error message, just as you would with printf
.
Example:
exception_type | The type of the exception to throw. |
format | The error message. |
... | A list of arguments that will be formatted according to format . |
#define EXCEPTION |
Retrieves the last exception that was thrown.
Example:
#define EXCEPTION_IS_UNCAUGHT |
Determines whether the thrown exception was not caught.
An exception is considered "uncaught" if no matching CATCH or CATCH_ALL block has been executed for it. In other words, this macro evaluates to a truthy value if the exception has bypassed all specific exception-handling logic and is propagating further. And it evaluates to a falsy value if no exception was thrown in the TRY block, or if an exception was successfully caught.
Example:
#define EXCEPTION_PRINT |
#define EXCEPTION_RETHROW |
Throws the current exception again.
typedef const char* e4c_exception_type |
|
extern |
Contains the current status of exceptions.
You MUST define this global variable for your program.