Look at the following code:
Code:
#include<stdio.h>
#include<stdint.h>
#include<stdlib.h>
#include<stdbool.h>
#include<setjmp.h>
#include<string.h>
typedef struct {
jmp_buf jump_buffer;
uint32_t error_code;
char *error_desc;
} exception_t;
void exception_throw(exception_t * e, uint32_t error_code, char *error_desc)
{
e->error_code = error_code;
e->error_desc = error_desc;
longjmp(e->jump_buffer, 1);
}
bool exception_catch(exception_t * e)
{
return setjmp(e->jump_buffer);
}
int main()
{
exception_t ex;
memset(&ex, 0, sizeof(exception_t));
if (exception_catch(&ex)) {
printf("exception thrown: %s", ex.error_desc);
return ex.error_code;
} else {
exception_throw(&ex, 1337, "fun");
}
return 0;
}
Why?