micro-test-plus 4.1.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
Checking Exceptions

Functions for verifying exceptions in test cases. More...

Functions

template<class Callable_T>
constexpr auto micro_os_plus::micro_test_plus::nothrow (const Callable_T &func)
 Check if a callable does not throw an exception.
template<class Callable_T>
constexpr auto micro_os_plus::micro_test_plus::throws (const Callable_T &func)
 Check if a callable throws an exception (any exception).
template<class Exception_T, class Callable_T>
constexpr auto micro_os_plus::micro_test_plus::throws (const Callable_T &func)
 Check if a callable throws a specific exception.

Detailed Description

Dedicated exception-check utilities verify whether expressions or function calls throw. These utilities support robust testing of error handling and exceptional conditions in C++ code.

Developers can assert that exceptions are thrown, or not thrown, as expected, improving the reliability and safety of software components. The framework supports both generic checks and type-specific verification, allowing precise and expressive test cases.

For advanced scenarios, such as handling multiple expected exception types, use explicit try blocks with multiple catch statements and report results with expect(true) or expect(false). This approach enables comprehensive coverage of exception-handling logic.

Examples
ts.test ("Check if exceptions are thrown", [] (auto& t) {
t.expect (mt::throws ([] { exercise_throw (true); }))
<< "exception thrown";
t.expect (mt::throws<std::runtime_error> ([]
{ throw std::runtime_error{ "" }; }))
<< "std::runtime_error thrown";
t.expect (mt::nothrow ([] { exercise_throw (false); }))
<< "exception not thrown";
});
ts.test ("Check exceptions explicitly", [] (auto& t) {
try
{
compute_answer ();
}
catch (const std::overflow_error& e)
{
t.expect (true) << "std::overflow_error thrown";
}
catch (const std::runtime_error& e)
{
t.expect (true) << "std::runtime_error thrown";
}
catch (...)
{
t.expect (false) << "unknown exception thrown";
}
});

Function Documentation

◆ nothrow()

template<class Callable_T>
auto micro_os_plus::micro_test_plus::nothrow ( const Callable_T & func)
nodiscardconstexpr
Template Parameters
Callable_TThe type of the callable object to be invoked.
Parameters
[in]funcThe callable object to check for exception safety.
Returns
An output stream to write optional messages.

The nothrow function template verifies whether invoking the provided callable object does not result in the throwing of any exception within the µTest++ framework. This is useful for testing exception safety and ensuring that code under test does not unexpectedly signal error conditions.

The function returns an output stream, allowing optional messages to be appended to the test report for diagnostic purposes.

Definition at line 115 of file exceptions-inline.h.

116 {
117 return detail::nothrow_<Callable_T>{ func };
118 }
Operator struct template to check if an expression does not throw any exception.
Definition detail.h:702

◆ throws() [1/2]

template<class Callable_T>
auto micro_os_plus::micro_test_plus::throws ( const Callable_T & func)
nodiscardconstexpr
Template Parameters
Callable_TThe type of the callable object to be invoked.
Parameters
[in]funcThe callable object to check for exception throwing behaviour.
Returns
An output stream to write optional messages.

The throws function template verifies whether invoking the provided callable object results in the throwing of any exception within the µTest++ framework. This is useful for testing general exception safety and ensuring that code under test properly signals error conditions.

The function returns an output stream, allowing optional messages to be appended to the test report for diagnostic purposes.

Definition at line 97 of file exceptions-inline.h.

98 {
99 return detail::throws_<Callable_T>{ func };
100 }
Operator struct template to check if an expression throws a specific exception.
Definition detail.h:645

◆ throws() [2/2]

template<class Exception_T, class Callable_T>
auto micro_os_plus::micro_test_plus::throws ( const Callable_T & func)
nodiscardconstexpr
Template Parameters
Exception_TThe type of the exception expected to be thrown.
Callable_TThe type of the callable object to be invoked.
Parameters
[in]funcThe callable object to check for exception throwing behaviour.
Returns
An output stream to write optional messages.

The throws function template verifies whether invoking the provided callable object results in the throwing of a specific exception type within the µTest++ framework. This is useful for testing error handling and exception safety in code under test.

The function returns an output stream, allowing optional messages to be appended to the test report for diagnostic purposes.

Definition at line 80 of file exceptions-inline.h.

81 {
83 }