exceptions4c 4.0
Exceptions for C
|
An exception handling library for C. More...
#include <stdlib.h>
#include <setjmp.h>
Data Structures | |
struct | e4c_exception_type |
Represents a category of problematic situations in a program. More... | |
struct | e4c_debug_info |
Contains debugging information about an exception. More... | |
struct | e4c_exception |
Represents a specific occurrence of an exceptional situation in a program. More... | |
struct | e4c_context |
Contains the configuration and the current status of exceptions. More... | |
Macros | |
#define | EXCEPTIONS4C 4 |
Returns the major version number of this library. | |
#define | TRY |
Introduces a block of code that may throw exceptions during execution. | |
#define | CATCH(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(type, format, ...) |
Throws an exception, interrupting the normal flow of execution. | |
#define | RETRY(max, type, format, ...) |
Repeats the previous TRY or USE block entirely. | |
#define | USING(acquisition, test, disposal) |
Introduces a block of code with automatic resource acquisition and disposal. | |
#define | WITH(disposal) |
Opens a block of code with automatic resource disposal. | |
#define | USE(test) |
Closes a block of code with automatic resource disposal. | |
#define | REACQUIRE(max, type, format, ...) |
Repeats the previous WITH or USING block entirely. | |
Functions | |
void | e4c_set_context_supplier (struct e4c_context *(*supplier)(void)) |
Sets the exception context supplier. | |
struct e4c_context * | e4c_get_context (void) |
Supplies the current exception context. | |
const struct e4c_exception * | e4c_get_exception (void) |
Retrieves the last exception that was thrown. | |
bool | e4c_is_uncaught (void) |
Determines whether the thrown exception was not caught. | |
An exception handling library for C.
This library consists of two files:
To use it in your project, include the header file in your source code files.
And then link your program against the library code.
#define EXCEPTIONS4C 4 |
Returns the major version number of this library.
#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 | ( | 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 (or a supertype of) 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
.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, USING, or WITH 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 | ( | type, | |
format, | |||
... ) |
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.
printf
. If no message is specified, then the default message for the supplied type
will be used.Example:
type | The type of the exception to throw. |
format | The error message. |
... | An optional list of arguments that will be formatted according to format . |
#define RETRY | ( | max, | |
type, | |||
format, | |||
... ) |
Repeats the previous TRY or USE block entirely.
This macro restarts the previous TRY or USE block up to a specified maximum number of attempts, or throws an exception if it has already been restarted too many times.
Example:
max | The maximum number of attempts. |
type | The type of exception to throw if max is exceeded. |
format | The error message. |
... | An optional list of arguments that will be formatted according to format . |
#define USING | ( | acquisition, | |
test, | |||
disposal ) |
Introduces a block of code with automatic resource acquisition and disposal.
Use this macro to easily acquire, use, and dispose of a resource.
A USING block is similar to a for statement, because it receives three comma-separated expressions that will be evaluated in order: acquisition
, test
, and disposal
.
Both these expressions and the code block that uses the resource MAY throw exceptions.
A single USING block MAY be followed by one or more CATCH, an optional CATCH_ALL block, and an optional FINALLY (in this order), just like TRY blocks.
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:
acquisition | The expression to acquire the resource. |
test | The condition for executing the code block. |
disposal | The expression to dispose of the resource. |
#define WITH | ( | disposal | ) |
Opens a block of code with automatic resource disposal.
You MAY use this macro instead of USING when the steps to acquire a resource are more complex than simply evaluating an expression.
A WITH block MUST be followed by one USE block.
Any of these blocks MAY throw exceptions.
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:
disposal | The expression to dispose of the resource. |
#define USE | ( | test | ) |
Closes a block of code with automatic resource disposal.
You MUST use this macro to close a preceding WITH block.
A USE block MUST be preceded by one WITH block and MAY be followed by one or more CATCH, an optional CATCH_ALL block, and an optional FINALLY (in this order), just like TRY blocks.
Any of these blocks MAY throw exceptions.
goto
, break
, continue
, or return
.Example:
test | The condition for executing the code block. |
#define REACQUIRE | ( | max, | |
type, | |||
format, | |||
... ) |
Repeats the previous WITH or USING block entirely.
This macro restarts the previous WITH or USING block, up to a specified maximum number of attempts, or throws an exception if it has already been restarted too many times.
Example:
max | The maximum number of attempts. |
type | The type of exception to throw if max is exceeded. |
format | The error message. |
... | An optional list of arguments that will be formatted according to format . |
void e4c_set_context_supplier | ( | struct e4c_context *(* | supplier )(void) | ) |
Sets the exception context supplier.
The library relies on this context to handle the current status of exceptions. If no supplier is provided, a default one will be used.
Example:
supplier | The function that supplies the current exception context. |
struct e4c_context * e4c_get_context | ( | void | ) |
Supplies the current exception context.
Example:
const struct e4c_exception * e4c_get_exception | ( | void | ) |
Retrieves the last exception that was thrown.
Example:
bool e4c_is_uncaught | ( | void | ) |
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 function returns true
if the exception has bypassed all specific exception-handling logic and is propagating further. And it returns false
if no exception was thrown in the TRY block, or if an exception was successfully caught.
Example:
true
if the current exception (if any) has not yet been handled by any CATCH or CATCH_ALL block; false
otherwise.