My Project
|
Abstract base class for all Unity exceptions. More...
#include <unity/Exception.h>
Public Member Functions | |
virtual char const * | what () const noexcept |
Returns a string describing the exception, including any exceptions that were nested or chained. More... | |
virtual std::exception_ptr | self () const =0 |
Returns a std::exception_ptr to this . More... | |
std::string | name () const |
Returns the name set by the derived class's constructor. | |
std::string | reason () const |
Returns the reason set by the derived class's constructor (empty string if none). More... | |
std::string | to_string (std::string const &indent=" ") const |
Returns a string describing the exception, including any exceptions that were nested or chained. More... | |
std::string | to_string (int indent_level, std::string const &indent) const |
Returns a string describing the exception, including any exceptions that were nested or chained. More... | |
std::exception_ptr | remember (std::exception_ptr earlier_exception) |
Adds an exception to the exception history chain. More... | |
std::exception_ptr | get_earlier () const noexcept |
Returns the previous exception. More... | |
Protected Member Functions | |
Exception (std::string const &name, std::string const &reason) | |
Constructs an exception instance. More... | |
Abstract base class for all Unity exceptions.
This class is the base class for all Unity exceptions. Besides providing a common base class for structured exception handling, this class provides features to capture nested exceptions (for exceptions that are re-thrown) and to chain exceptions into an exception history that allows a number of exceptions to be remembered before throwing a new exception.
The exception nesting is provided by the derivation from std::nested_exception
. If you catch an exception and throw another exception from the catch handler, the caught exception is automatically preserved; you can access nested exceptions by calling the nested_ptr()
and rethrow_nested()
member functions of std::nested_exception
.
In addition, you can remember one or more exceptions by calling remember(). This is useful in situations where you need to perform a number of actions that may fail with an error code, and you do not want to throw an exception until all of the actions have been attempted. This is particularly useful in shutdown scenarios, where it is often impossible to recover from an error, but it is still desirable to try to shut down as much as possible before reporting or logging the errors:
Calling what() on a caught exception returns a string with the entire exception history (both nested and chained).
|
protected |
Constructs an exception instance.
name | The fully-qualified name of the exception, such as "unity::SyscallException" . The name must not be empty. |
reason | Any additional details about the exception, such as an error message and the values of any members of the exception. |
|
noexcept |
Returns the previous exception.
nullptr
, if none. string unity::Exception::reason | ( | ) | const |
Returns the reason set by the derived class's constructor (empty string if none).
Derived classes should include any other state information, such as the value of data members or other relevant detail in the reason
string they pass to the protected constructor.
exception_ptr unity::Exception::remember | ( | std::exception_ptr | earlier_exception | ) |
Adds an exception to the exception history chain.
earlier_exception | The parameter must be a nullptr or a std::exception_ptr to an exception that was remembered earlier. This allows a sequence of exceptions to be remembered without having to throw them and is useful, for example, in shutdown scenarios where any one of a sequence of steps can fail, but we want to continue and try all the following steps and only throw after all of them have been tried. In this case, each step that fails can add itself to the sequence of remembered exceptions, and finally throw something like ShutdownException . |
std::exception_ptr
to this
.
|
pure virtual |
Returns a std::exception_ptr
to this
.
std::exception_ptr
to its own derived exception. Implemented in unity::ResourceException, unity::SyscallException, unity::FileException, unity::ShutdownException, unity::LogicException, and unity::InvalidArgumentException.
string unity::Exception::to_string | ( | std::string const & | indent = " " | ) | const |
Returns a string describing the exception, including any exceptions that were nested or chained.
Nested exceptions are indented according to their nesting level. If the exception contains chained exceptions, these are shown in oldest-to-newest order.
indent | This controls the amount of indenting per level. The default indent is four spaces. |
to_string(0, indent)
. string unity::Exception::to_string | ( | int | indent_level, |
std::string const & | indent | ||
) | const |
Returns a string describing the exception, including any exceptions that were nested or chained.
Nested exceptions are indented according to their nesting level. If the exception contains chained exceptions, these are shown in oldest-to-newest order.
indent_level | This controls the indent level. The value 0 indicates the outermost level (no indent). |
indent | This controls the amount of indenting per level. The passed string is prependended indent_level times to each line. |
|
virtualnoexcept |
Returns a string describing the exception, including any exceptions that were nested or chained.