Exceptions in C!

This commit is contained in:
vegowotenks 2025-06-13 16:30:32 +02:00
parent e8bea8924b
commit 26179d76aa
6 changed files with 134 additions and 0 deletions

View file

@ -60,4 +60,8 @@ target_link_libraries(QuadTree-test
rand
pointers
allocator-interface
add_executable(ExceptionalScope-test ExceptionalScope.test.c)
target_link_libraries(ExceptionalScope-test
ExceptionalScope
)

View file

@ -0,0 +1,44 @@
#include "../src/ExceptionalScope/ExceptionalScope.h"
#include <stdio.h>
#define unsafe
void print(const char* s)
{
unsafe {
puts(s);
}
}
int main()
{
ExceptionalScope scope;
ExceptionalScope_Create(&scope);
scope.exception = (void*) 0xCAFEBABE;
try(&scope) {
ExceptionalScopeCleaner outerCleaner = {
.next = NULL,
.arg = "Unwinding the cleaner stack #2",
.action = (ExceptionalScopeCleanerFunction) print,
};
withScopedCleaner(&scope, outerCleaner) {
ExceptionalScopeCleaner innerCleaner = {
.next = NULL,
.arg = "Unwinding the cleaner stack #1",
.action = (ExceptionalScopeCleanerFunction) print,
};
withScopedCleaner(&scope, innerCleaner) {
ExceptionalScope_Throw(&scope, (void*) 0xDEADBEEF);
puts("After the throw");
}
}
} catch {
printf("Caught an exception: %p\n", scope.exception);
}
ExceptionalScope_Destroy(&scope);
}