Files
mem/tests/03_exception.c

29 lines
645 B
C
Raw Normal View History

2026-03-06 08:51:02 -06:00
#define DEBUG
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include "../include/mem.h"
int main(void){
size_t nbytes = MEM_GB(1024);
void *buf;
TRY {
buf = ALLOC(nbytes); /* try 1GB */
if (!buf)
RAISE(oom);
free(buf);
}
EXCEPT(oom) {
/* handle memory failure gracefully */
fprintf(stderr, "Caught: %s\n", oom.reason);
return EXIT_SUCCESS; /* requested behavior */
}
FINALLY {
/* cleanup if needed, runs always */
if (buf) free(buf);
}
END_TRY;
return EXIT_FAILURE; /* shouldn't reach here for this example */
}