utilitiec/tests/ExceptionalScope.test.c
2025-06-13 16:30:32 +02:00

44 lines
897 B
C

#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);
}