Exceptions in C

It's currently in the alpha stage (read: there are lots of bugs, it depends on gcc + glibc, and probably only works on Linux (maybe on Windows with Cygwin or even MinGW, but certainly not with MSVC)) and some of the code is very ugly, but I got it partially working.

'try' and 'catch' work, 'otherwise' doesn't work, and 'finally' hasn't been implemented yet. It also leaks memory because I can't think of a reliable way to free exception structures automatically without freeing them before the user gets an opportunity to use them.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <exception.h>

int idiv(int numerator, int denominator)
{
	if (denominator == 0)
		throw(EXCEPTION_DIVISION_BY_ZERO, "Integer division by zero");
	return numerator / denominator;
}

int main()
{
	try {
		idiv(1, 0);
	} catch (EXCEPTION_ANY) {
		exception_print(e);
	} otherwise {
		printf("No exceptions occurred.\n");
	} finally {
		/* Clean-up code */
	}
	return 0;
}
Exception thrown: "division by zero" exception occurred:
	src/ex002.c:6: idiv: Integer division by zero
Stack trace:
	 1. /home/chris/Projects/Experiments/exCeptions/lib/libexception.so.0(+0x10ba) [0x7f5cef9250ba]
	 2. /home/chris/Projects/Experiments/exCeptions/lib/libexception.so.0(__throw+0x12d) [0x7f5cef92521c]
	 3. examples/bin/ex002() [0x40082c]
	 4. examples/bin/ex002() [0x400879]
	 5. /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed) [0x7f5cef5a630d]
	 6. examples/bin/ex002() [0x400739]

The stacktrace isn't very good, but there's nothing I can do about it AFAIK.

Source, binaries, etc.: http://www.mediafire.com/?1e24lxrc4u741rc

Note: You need to put LD_LIBRARY_PATH=/where/you/extracted/the/archive/lib:$LD_LIBRARY_PATH before the name of the program to run an example.
Last edited on
Exceptions in a language that doesn't have any means of automatic cleanup? Seems like a bad idea.

It's hard enough being exception safe in C++. In C it seems like it'd just be nuts. Every function that does any sort of allocation would have to be put in a try block.
Last edited on
It was an idea, I never said it was a good idea! :P

Also, it uses setjmp/longjmp... like goto, only worse.

p.s. one of my favourite things to do in C is add things the language doesn't support by default. I've done garbage collection and now exceptions. Next are OOP (I tried this before but it didn't quite work) and FP.
Last edited on
closed account (1yR4jE8b)
Sooo....turning it into C++?
Not quite, more turning it into an amalgamation of several languages.
Also, in a way, I'm learning how those things work.
I suggest you look at the source code of C++ compilers such as GCC to see how they implement this.
However, I have never heard about otherwise and finally in C++ exception.
I prefer a more practical approach :)

If you've ever looked at other languages with exception handling, some of them (Python and C#, for example) have an 'else' clause, which is what to do when an exception hasn't occurred. I would have just used else, but I needed an extra brace (like try { } catch (...) { } } else { }) because of the way 'catch' is defined. Obviously I couldn't redefine 'else's, I had to make a new one.

Python and C# also support a 'finally' clause, which is code to run before exiting the function, regardless of whether an exception was thrown. I haven't implemented it because I don't know how.
Topic archived. No new replies allowed.