cexcept

Cosmin Truta and I have developed a try/catch/throw exception-handling interface for C. It basically works the way you would expect:

    Try {
        /* Maybe a Throw statement will be executed    */
        /* (maybe within a deeply nested function):    */
        /* Throw e;                                    */
        /* If so, execution jumps to the catch clause. */
    }
    Catch (e) {
        /* An exception was thrown and copied into e.  */
    }

The interface of the latest release is fully implemented and documented in cexcept.h. It is thread-safe, and the programmer has the flexibility (and burden) of specifying the exception type.

Source code

You can get a tarball or zip file containing the latest (2008-Jul-23-Wed) release of cexcept.h and accompanying example code, etc, or browse through the various versions of the individual files:

Mailing list

There used to be a mailing list for discussion of cexcept, but the list server didn't survive a server migration, and apparently no one noticed. I'd be happy to resurrect the mailing list if anyone is interested, just ask me.

To browse the list archive, enter “cexcept” in the form below and submit it.

Keyword:

This project originally grew out of a discussion of error handling in libpng (the PNG reference library), but is now independent of libpng.

Similar projects

Doug Jones's exception handling interface, like cexcept, uses only a small .h file, but it makes some different design tradeoffs (it is modeled after the Ada programming language).

Doug Gwyn's Ex package uses both a small .h file and a small .c file that defines a global variable (not suitable for multi-threaded programs). The exception type is a integer.

Andrew Snowden's LIBXC likewise uses a small .h and a small .c file that defines a global variable. The exception type is a fixed structure with some typically useful members including filename and line number. Uncaught exceptions elicit helpful messages. The package also includes libc function wrappers that throw exceptions rather than return unsuccessfully.

Ian Zimmerman's exc package uses a larger .c file and .h file and provides more features, like built-in support for polymorphic exceptions and multiple catch clauses, and detection of programmer errors like jumping out of a try clause [to accomplish this, the keyword return is redefined, which is very clever, although at most one package could use this trick at a time]. The exc package is contained inside the larger gim package (in libgim/exc.*), formerly available from its author at http://www.speakeasy.net/~itz/hacks/. Apparently the last version was gim-0.9.4.tar.gz, which is still mirrored at various sites.

Bruce W. Bigby's General Exception-Handling Faciliity (GEF) uses three .c files and three .h files to provide not only exception handling but also design-by-contract support (invariants, preconditions, postconditions, assertions). It is modeled after the Eiffel programming language.

The except.h and except.c files in chapter “Exceptions and Assertions” of the book C Interfaces and Implementations.

Nicolas Laurent's nucleotides project includes src/exceptions.c and doc/exceptions.md. It supports a hierarchy of exception types, and supports finally clauses at the cost of disallowing jumping out of catch clauses.

Related projects

Mikael Lyngvig's C with exceptions paper proposes a way to add efficient exception support to the C language, by modifying compilers. It has been implemented as a patch to the Tiny C Compiler.


[AMC]  Prepared by Adam M. Costello
 Last modified: 2012-Oct-29-Mon 05:40:46 GMT
[Any Browser]