Logo Search packages:      
Sourcecode: virtualbox-ose version File versions

stdx::exception_trap< T > Class Template Reference

#include <cpputils.h>

Inheritance diagram for stdx::exception_trap< T >:

stdx::exception_trap_base

List of all members.


Detailed Description

template<typename T>
class stdx::exception_trap< T >

The exception_trap template acts like a wrapper for the given exception class that stores a full copy of the exception and therefore allows to rethrow it preserving the actual type information about the exception class.

This functionality is useful in situations where it is necessary to catch a (known) number of exception classes and pass the caught exception instance to an upper level using a regular variable (rather than the exception unwinding mechanism itself) *and* preserve all information about the type (class) of the caight exception so that it may be rethrown on the upper level unchanged.

Usage pattern:

    using namespace std;
    using namespace stdx;

    auto_ptr <exception_trap_base> trapped;

    int callback();

    int safe_callback()
    {
      try
      {
        // callback may throw a set of exceptions but we don't want it to start
        // unwinding the stack right now

        return callback();
      }
      catch (const MyException &err) { trapped = new_exception_trap (err); }
      catch (const MyException2 &err) { trapped = new_exception_trap (err); }
      catch (...) { trapped = new_exception_trap (logic_error()); }

      return -1;
    }

    void bar()
    {
      // call a funciton from some C library that supports callbacks but knows
      // nothing about exceptions so throwing one from a callback will leave
      // the library in an undetermined state

      do_something_with_callback (safe_callback());

      // check if we have got an exeption from callback() and rethrow it now
      // when we are not in the C library any more
      if (trapped.get() != NULL)
        trapped->rethrow();
    }

Parameters:
T Exception class to wrap.

Definition at line 314 of file cpputils.h.


Public Member Functions

 exception_trap (const T &aTrapped)
void rethrow ()

Public Attributes

trapped

The documentation for this class was generated from the following file:

Generated by  Doxygen 1.6.0   Back to index