BeRTOS
Defines | Variables
except.h File Reference

C++-like structured exception handling for C programs. More...

#include <cfg/debug.h>
#include <setjmp.h>

Go to the source code of this file.

Defines

#define TRY   if (PUSH_EXCEPT) { {
 Jump buffer to use when throwing an exception or aborting an operation.

Variables

jmp_buf except_stack [EXCEPT_CONTEXTS]
 A stack of jump buffers used to record try sites so they can be reached from throw sites.

Detailed Description

C++-like structured exception handling for C programs.

Author:
Bernie Innocenti <bernie@codewiz.org>

Definition in file except.h.


Define Documentation

#define TRY   if (PUSH_EXCEPT) { {

Jump buffer to use when throwing an exception or aborting an operation.

User code can throw exceptions like this:

   void a_function_throwing_exceptions(void)
   {
       if (some_error_condition)
          THROW;
   }

Catching exceptions (brackets are optional):

    EXCEPT_DEFINE;

    void a_function_catching_an_exception(void)
    {
        TRY
        {
            printf("Entered try block\n");
            a_function_throwing_exceptions();
            printf("Survived execution of critical code\n");
        }
        CATCH
        {
            printf("Exception caught!\n");
        }
        CATCH_END
    }

Simple syntax when you don't need to do anything when catching an excaption:

    TRY
        printf("Entered try block\n");
        a_function_throwing_exceptions();
        printf("Survived execution of critical code\n");
    TRY_END

You also need to declare the exception stack once in your global declarations:

    EXCEPT_DEFINE;

Definition at line 113 of file except.h.


Variable Documentation

jmp_buf except_stack[EXCEPT_CONTEXTS]

A stack of jump buffers used to record try sites so they can be reached from throw sites.

The stack contains return points for each nested context. jmp_buf's are pushed into the stack at try points and popped out when the try block ends normally or when an exception is thrown.