Try/Catch Exception implementation for C Programming Language
In order to build you have to link trycatch.c, but you can bypass this by defining EXCEPTION_CONTEXT. See test/custom.c.
gcc -I/potato /potato/trycatch.c test.c /potato/trycatch.c -o test.exe
#include <stdio.h>
#include <trycatch.h>
enum {
E_MYEXCEPTION = 72,
};
int might_throw() {
throw(E_MYEXCEPTION, "Something went wrong");
return 2;
}
int main() {
try {
int a = might_throw();
} catch (Exception e) {
if (e.code != E_MYEXCEPTION) {
printf("Unknown exception\n");
return -1;
}
// handle exception
printf("MyException: %s\n", (char*)e.data);
}
return 0;
}For more examples see the tests
This is just a proof of concept, but I would like to adjust it as much as possible; any feedback, suggestion and improvement is welcomed in the issues page.
Thank you.