Exceptions in C!
This commit is contained in:
parent
e8bea8924b
commit
26179d76aa
6 changed files with 134 additions and 0 deletions
|
@ -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
|
||||
)
|
||||
|
|
44
tests/ExceptionalScope.test.c
Normal file
44
tests/ExceptionalScope.test.c
Normal 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue