Static library containing macros for throwing and handling exceptions in C based on the C# syntax.
Unhandled exceptions result in a call to abort()
.
To respond to unhandled exceptions, the SIGABRT
signal can be handled or a try/catchAll block can be defined in the main function.
Interface reference:
throw
: throw an exception- id - ID of the exception. Can be an
ExceptionID
value or an integer. - message - Message of the exception. Must be a string (
char *
).
- id - ID of the exception. Can be an
try
: define a try blockcatch
: catch an exception with a specific ID- targetId - ID of the exception to catch. Can be an
ExceptionID
value or an integer. - varName - Name of the variable (of type
Exception const *const
) that points the catched exception.
- targetId - ID of the exception to catch. Can be an
catchWhen
: catch an exception with a specific ID only when the provided boolean expression evaluates to true- targetId - ID of the exception to catch. Can be an
ExceptionID
value or an integer. - varName - Name of the variable (of type
Exception const *const
) that points the catched exception.
- targetId - ID of the exception to catch. Can be an
catchAll
: catch any exception- varName - Name of the variable (of type
Exception const *const
) that points the catched exception.
- varName - Name of the variable (of type
catchAllWhen
: catch any exception when the provided boolean expression evaluates to true- varName - Name of the variable (of type
Exception const *const
) that points the catched exception.
- varName - Name of the variable (of type
rethrow
: rethrow an exception from a catch block.finally
: defines a block to execute regardless of whether an exception occured or not.
See the comments in example for a walk-through of the code.
- The
try
macro cannot be used in a single-statement scope (i.e.if
statement without braces). This is not a problem since it is useless without catch or finally blocks. - An exception variable name has to be specified when defining a catch block. You can use
_
as the identifier if you're not planning to use it. Unused variable warnings will be hidden if GNU C is supported (with theunused
attribute). - The try-catch stack is defined as thread-local, so exceptions will not bubble up outside of their original thread.
All reserved identifiers are prefixed by _ex_
.
This is mostly an experiment with advanced macro features and the setjmp
/longjmp
.