Skip to main content

Checking Exceptions

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

Functions Index

template <class Callable_T>
constexpr autonothrow (const Callable_T &func)

Check if a callable does not throw an exception. More...

template <class Callable_T>
constexpr autothrows (const Callable_T &func)

Check if a callable throws an exception (any exception). More...

template <class Exception_T, class Callable_T>
constexpr autothrows (const Callable_T &func)

Check if a callable throws a specific exception. More...

Description

Functions for verifying exceptions in test cases.

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";
  }
 });

Functions

nothrow()

template <class Callable_T>
auto micro_os_plus::micro_test_plus::nothrow (const Callable_T & func)
nodiscard constexpr

Check if a callable does not throw an exception.

Template Parameters
Callable_T

The type of the callable object to be invoked.

Parameters
[in] func

The 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.

115 nothrow (const Callable_T& func)
116 {
117 return detail::nothrow_<Callable_T>{ func };
118 }

throws()

template <class Callable_T>
auto micro_os_plus::micro_test_plus::throws (const Callable_T & func)
nodiscard constexpr

Check if a callable throws an exception (any exception).

Template Parameters
Callable_T

The type of the callable object to be invoked.

Parameters
[in] func

The 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.

97 throws (const Callable_T& func)
98 {
99 return detail::throws_<Callable_T>{ func };
100 }

throws()

template <class Exception_T, class Callable_T>
auto micro_os_plus::micro_test_plus::throws (const Callable_T & func)
nodiscard constexpr

Check if a callable throws a specific exception.

Template Parameters
Exception_T

The type of the exception expected to be thrown.

Callable_T

The type of the callable object to be invoked.

Parameters
[in] func

The 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.

80 throws (const Callable_T& func)
81 {
83 }

Generated via doxygen2docusaurus 2.2.0 by Doxygen 1.17.0.