C/C++ setjmp/longjmp

Created 2025-05-06

#include
#include 

jmp_buf buf;

void func()
{
	puts("hello from func()");
	longjmp(buf, 1); // jump to the point setup by setjmp
	puts("I am never printed");
}

int main()
{
	if(setjmp(buf) == 0) {
		//try
		puts("try");
		func();
	} else {
		// catch
		puts("catch");
	}

	return 0;
}

Output is:

try
hello from func()
catch