ExceptionHandling.h File Reference

Java-like exceptions for C. More...

#include <setjmp.h>
#include <string.h>

Go to the source code of this file.

Defines

#define try
 Start execution of code which might throw an exception that might be caught by the exception handlers that follow this try.
#define catch(X)
 Define an exception handler.
#define catchAll()
 Define a default exception handler.
#define endtry
 End exception handling.
#define throw(X)
 Throw an exception.
#define rethrow()   throw(exception)
 Rethrows the current exception.
#define throws(X)
 Declare that a function throws an exception of this type.

Variables

jmp_buf * currentExceptionBuffer
 The exception buffer of the current thread.


Detailed Description

Java-like exceptions for C.

This is part of AceUnit but can also be used without AceUnit.

Example:

 #define SomeException 1
 int someMethod() throws(SomeException) {
     if (someCondition) {
         throw(SomeException);
     }
 }

 int someCaller() {
     try {
         someMethod();
     } catch (SomeException) {
         // handle this exception
     } endtry;
 }
 

Differences to Java

Similarities with Java

Author:
Christian Hujer

Define Documentation

#define catch (  ) 

Value:

while (false); \
            break; \
        case (X): exception = 0; do \
Define an exception handler.

Parameters:
X exception to catch, MUST be of type int.

 
#define catchAll (  ) 

Value:

while (false); \
            break; \
        default: exception = 0; do \
Define a default exception handler.

#define endtry

Value:

while (false); \
        } \
        currentExceptionBuffer = prev; \
        if (exception) { \
            longjmp(*currentExceptionBuffer, exception); \
        } \
    } while(0)
End exception handling.

 
#define rethrow (  )     throw(exception)

Rethrows the current exception.

This is a shorthand for throw(exception).

#define throw (  ) 

Value:

do { \
        longjmp(*currentExceptionBuffer, (X)); \
    } while (false)
Throw an exception.

Parameters:
X Exception to throw, MUST be of type int.

#define throws (  ) 

Declare that a function throws an exception of this type.

This is currently unused but might be used in future by additional tools that could look for uncaught exceptions.

Parameters:
X Exception type to declare, MUST be of type int.

#define try

Value:

do { \
        int exception; \
        jmp_buf lbuf; \
        jmp_buf *prev = currentExceptionBuffer; \
        currentExceptionBuffer = &lbuf; \
        switch (exception = setjmp(lbuf)) { \
        case 0: do \
Start execution of code which might throw an exception that might be caught by the exception handlers that follow this try.

From catch / catchAll to endtry, a local variable named exception will exist and contain the exception information (that is, setjmp() return value).

Warning:
Never omit endtry


Variable Documentation

jmp_buf* currentExceptionBuffer

The exception buffer of the current thread.

If you work in a multi-threaded environment, make sure the scheduler allocates a jmp_buf for each thread and initializes currentExceptionBuffer for that thread with each conext switch. If you don't, your system will get very upset about interrupted exceptions.

Make sure that the scheduler starts each thread with an initialized jmp_buf (invoke setjmp() once!), otherwise your system will get very upset about uncaught exceptions.

If you work in a single-threaded environment, still make sure that setjmp(*currentExceptionBuffer) is invoked at least once at the very beginning, otherwise your system will get very upset about uncaught exceptions.


Generated on Sun Feb 13 11:10:16 2011 for AceUnit by  doxygen 1.5.3