#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. |
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
#define catch | ( | X | ) |
Value:
while (false); \ break; \ case (X): exception = 0; do \
X | exception to catch, MUST be of type int. |
#define catchAll | ( | ) |
Value:
while (false); \ break; \ default: exception = 0; do \
#define endtry |
Value:
while (false); \ } \ currentExceptionBuffer = prev; \ if (exception) { \ longjmp(*currentExceptionBuffer, exception); \ } \ } while(0)
#define rethrow | ( | ) | throw(exception) |
Rethrows the current exception.
This is a shorthand for throw(exception).
#define throw | ( | X | ) |
Value:
do { \ longjmp(*currentExceptionBuffer, (X)); \ } while (false)
X | Exception to throw, MUST be of type int. |
#define throws | ( | X | ) |
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.
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 \
From catch / catchAll to endtry, a local variable named exception will exist and contain the exception information (that is, setjmp() return value).
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.