Author: | Benedikt Böhm |
---|---|
Version: | 0.2_beta |
Web: | http://github.com/hollow/libexception |
Git: | git clone https://github.com/hollow/libexception.git |
Download: | http://github.com/hollow/libexception/downloads |
libexception is an exception handling library for C. It uses the setjmp
and
longjmp
calls to build a stack of jump buffers and exceptions and provides
high level semantics implemented as macros to make exception handling in C
unobtrusive and easy to use.
Error handling is an important issue in programming languages, and it can account for a substantial portion of a project's code. The classic C approach to this problem is return codes. Each function returns a value indicating success or failure. However, with a nontrivial function call hierarchy, this approach clutters the code significantly. Every function must check the return code of every function call it makes and take care of errors. In most cases, the function will merely pass any errors back up to its caller.
Programming languages such as Ada or C++ address this issue with exceptions. Exceptions make it easy to separate error handling from the rest of the code. Intermediate functions can completely ignore errors occurring in functions they call, if they can't handle them anyway.
The solution to this problem is to implement a simple exception-handling library in C.
libexception uses two stacks implemented as a doubly-linked list to record exceptions and jump
buffers needed by setjmp
/longjmp
. The work-flow is as follows:
try
will callsetjmp
and push the resultingjmp_buf
object onto the tryenv stack.- usually
setjmp
returns zero and the following block is executed. if no exception occured nothing else happens. - if an exception is raised
throw
will push information about the error onto the exception stack and calllongjmp
to jump to the topmost tryenv buffer. - in this case
setjmp
returns again, but with a non-zero return code and the followingexcept
block is executed. - an except block consists of zero or more
on
blocks to handle a single exception and zero or onefinally
block to handle a yet unhandled but unknown exception - if an
on
block handled the exception the exception stack is cleared and nothing else happens. - if no
on
block handled the exception but afinally
block handled the exception the exception stack is cleared and nothing else happens. - if an unhandled exception is found at the end of an
except
block, control is transfered to the next jump buffer (a.k.aexcept
block). - if there are no more jump buffers available, the default exception handler
is called to print an exception trace to
STDERR
and the program will be aborted.
To install libexception call ./configure
, then make
and finally make install
.
The libexception API reference can be found on-line or in PDF.
Examples can be found in the test-suite.