exceptions4c-lite 1.0
Lightweight exception handling for C
|
Bring the power of exceptions to your C applications with this header-only library!
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.
Create meaningful exceptions that reflect problematic situations in the program.
An exception type is tied to its default error message.
Exception handling lets a program deal with errors without crashing. When something goes wrong, the program pauses its normal flow, jumps to code that handles the issue, and then either recovers or exits cleanly.
This library provides the following macros that are used to handle exceptions:
When we THROW an exception, the flow of the program moves to the appropriate CATCH block. If the exception is not handled by any of the blocks in the current function, it propagates up the call stack to the function that called the current function. This continues until the top level of the program is reached. If no block handles the exception, the program terminates and an error message is printed to the console.
Use THROW to trigger an exception when something goes wrong.
If you don't provide an error message, the default one for that exception type will be used.
Use THROWF to trigger an exception with a formatted message, just as you would with printf
.
Use a TRY block to wrap code that might cause an exception.
These code blocks, by themselves, don't do anything special. But they allow the introduction of other blocks that do serve specific purposes.
goto
, break
, continue
, or return
.To prevent the program from crashing, exceptions need to be handled properly in designated sections of the code.
Use a CATCH block to handle a specific type of exceptions when they occur.
One or more CATCH blocks can follow a TRY block. Each CATCH block must specify the type of exception it handles. If its type doesn't match the thrown exception, then that block is ignored, and the exception may be caught by the following blocks.
On the other hand, the CATCH_ALL block is a special block that can handle all types of exceptions.
Only one CATCH_ALL block is allowed per TRY block, and it must appear after all type-specific CATCH blocks if any are present.
Use EXCEPTION to retrieve the exception currently being handled.
A FINALLY block always runs, no matter whether an exception happens or not.
This block is optional. And, for each TRY block, there can be only one FINALLY block. If an exception occurs, the FINALLY block is executed after the CATCH or block that can handle it. Otherwise, it is executed after the TRY block.
You can define these macros to customize exceptions4c-lite:
NDEBUG
is not defined and the debugging info is enabled.snprintf
are used.This library should compile in any ANSI C compiler.
Exception handling is based on standard C library functions setjmp
to save the current execution context and longjmp
to restore it. According to the documentation:
Upon return to the scope of
setjmp
:
- all accessible objects, floating-point status flags, and other components of the abstract machine have the same values as they had when
longjmp
was executed,- except for the non-volatile local variables in the function containing the invocation of
setjmp
, whose values are indeterminate if they have been changed since thesetjmp
invocation.
Since each TRY block invokes setjmp
, modified local variables in scope must be volatile
.
This library adheres to Semantic Versioning. All notable changes for each version are documented in a change log.
Head over to GitHub for the latest release.
The source code is available on GitHub.