1.4.3. Throwables

Throwing arbitrary exceptions is a bit of a problem since the type of the exception is determined at compile time. So if you want to store an exception and throw it some time later at a maybe unknown position in your code you must wrap it in an envelope and delegate the process of throwing to this wrapper.

Similar to the according Java classes they are called Throwables. mockpp provides several classes and functions to deal with single or lists of throwables as well as creating them from ordinary values.


  ThrowableItem it (make_throwable(float(1.234))); 1
  it.get()->throw_me(); 2

  ThrowableList lit; 3
  lit.push_back(make_throwable(std::string("data"))); 4
  lit.push_back(make_throwable(unsigned(123)));
  lit.nextThrowableObject()->throw_me(); 5

1

Create a throwable object with a float value as payload.

2

Throw it.

3

Create a list of throwable objects.

4

Add some objects to our list. Note that the objects have completely different types.

5

Get the first available object from the list and throw it.