This module offers a straightforward macro interface that facilitates seamless exception handling in the C programming language, drawing inspiration from the paradigm employed in Python.
- Open a terminal window and ensure that you have yay installed on your system.
- Run the command
yay -S except-c
to search for and install the package from the AUR (Arch User Repository).
- Download the source code for the
except-c
package from releases - Extract the contents of the downloaded archive to a directory of your choice.
- Open a terminal window and navigate to the directory where you extracted the source code.
- Run the command
make pkg
to build ax86_64.pkg.tar.zst
package, or you can run the commandmake compile
to build the filelibexcept.a
. - Then just install the pacakge.
- Install the except-c library on your system.
- Include the
except.h
header file in your C source code file using the following statement:
#include <except.h>
- Define your exceptions using the
Except_T
type, as demonstrated in the following example:
Except_T someError = INIT_EXCEPT_T("Some error");
In this instance, someError
represents an exception of the Except_T
type, accompanied by the message "Some error."
Alternatively, you can leverage the general-purpose exceptions already defined in the except.h header file,
depending on your specific requirements.
/* Logical exceptions */
extern T ExceptLogicalError;
extern T ExceptInvalidArgument;
extern T ExceptDomainError;
extern T ExceptLengthError;
extern T ExceptOutOfRange;
/* System error exceptions */
extern T ExceptRuntimeError;
extern T ExceptRangeError;
extern T ExceptOverflowError;
extern T ExceptSystemError;
extern T ExceptFileSystemError;
extern T ExceptThreadSystemError;
extern T ExceptSocketSystemError;
extern T ExceptReadSystemError;
extern T ExceptWriteSystemError;
/* Memory exceptions */
extern T ExceptBadAlloc;
extern T ExceptBadArrayNewLength;
extern T ExceptBadPtr;
/* Signal exceptions */
extern T ExceptSigAbrt;
extern T ExceptFpe;
extern T ExceptSigIll;
extern T ExceptSigSegv;
extern T ExceptSigTerm;
extern T ExceptSigSys;
- Use the TRY, END_TRY, RAISE, and EXCEPT macros to handle exceptions in your code. Here is an example code snippet:
TRY {
RAISE(someError); // <--- raise the exception of someError
} EXCEPT(someError) {
puts("Exception caught!");
} END_TRY;
This code snippet attempts to raise the someError exception using the throw macro. If the exception is raised, the except macro will catch it and execute the code block following it. In this case, the code block simply prints a message to the console. Additionally, you have the option to include a message when raising an exception to provide supplementary information about the error:
TRY {
RAISE(someError, "This is a message"); // <--- Adding an message
} EXCEPT(someError) {
puts("Exception caught!");
} END_TRY;
This modification allows you to add a descriptive message when throwing the someError
exception, enhancing the contextual
information available during exception handling and debugging.
- Compile your code with the -lexcept flag to link against the except-c library and with -ggdb to have the backtrace from your exceptions. For example:
cc -ggdb mycode.c -lexcept
2024-05-29_21-44-54.mp4
By including the except/assert.h
header, you override the default assert
functionality in C. With this new implementation, a failed assertion
will be treated as an exception.
- Include the
except/assert.h
header file in your C source code file using the following statement:
#include <except/assert.h>
- You can then use the
assert()
keyword to evaluate a condition and add a custom message.
assert(0, "This is a message"); // condition, message
- Compile your code with the -lexcept flag to link against the except-c library. For example:
cc -ggdb mycode.c -lexcept
This is an example output of an uncaught exception from a failed assertion.
[arch]$ cc -ggdb mycode.c -lexcept
[arch]$ ./a.out
Traceback...
File "assert.c", line 9, raised in bar()
File "/home/aleck/Documents/projects/except-c/examples/assert.c", line 15, called from foo()
File "/home/aleck/Documents/projects/except-c/examples/assert.c", line 23, called from main()
UncaughtException: "Assertion failed", "This is a message"
Aborting....
Aborted (core dumped)
- Mark. (2023). How setjmp and longjmp work. Offlinemark. https://offlinemark.com/2016/02/09/lets-understand-setjmp-longjmp/
- Hanson, D. (1996). C Interfaces and Implementations: Techniques for Creating Reusable Software.