Skip to content

bruneo32/trycatch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C straightforward exception handling

Try/Catch Exception implementation for C Programming Language

Use

In order to build you have to link trycatch.c, but you can bypass this by defining EXCEPTION_CONTEXT. See test/custom.c.

gcc -I/potato /potato/trycatch.c test.c /potato/trycatch.c -o test.exe

#include <stdio.h>
#include <trycatch.h>

enum {
	E_MYEXCEPTION = 72,
};

int might_throw() {
	throw(E_MYEXCEPTION, "Something went wrong");
	return 2;
}

int main() {
	try {
		int a = might_throw();
	} catch (Exception e) {
		if (e.code != E_MYEXCEPTION) {
			printf("Unknown exception\n");
			return -1;
		}

		// handle exception
		printf("MyException: %s\n", (char*)e.data);
	}

	return 0;
}

For more examples see the tests

Waiver

This is just a proof of concept, but I would like to adjust it as much as possible; any feedback, suggestion and improvement is welcomed in the issues page.

Thank you.

About

Try/Catch Exception implementation for C Programming Language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published