Skip to main content

The Checking Exceptions Reference

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

Functions Index

constexpr autonothrow (const Callable_T &func)

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

constexpr autothrows (const Callable_T &func)

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

constexpr autothrows (const Callable_T &func)

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

Description

Functions for verifying exceptions in test cases.

The µTest++ framework provides dedicated functions for verifying whether specific expressions or function calls throw exceptions, supporting robust testing of error handling and exceptional conditions in C++ code.

These utilities enable developers to assert that exceptions are correctly thrown or not thrown as expected, improving the reliability and safety of software components. The framework supports both generic exception checks and type-specific exception verification, allowing for precise and expressive test cases.

For more advanced scenarios, such as handling multiple expected exceptions, developers can use explicit try blocks with multiple catch statements and report the results using expect(true) or expect(false). This approach ensures comprehensive coverage of exception handling logic.

Examples
 mt::expect (mt::throws ([] { exercise_throw (true); })) << "exception thrown";
 
 mt::expect (mt::throws<std::runtime_error> ([] { throw std::runtime_error{ ""
 }; }))
  << "std::runtime_error thrown";
 
 mt::expect (mt::nothrow ([] { exercise_throw (false); })) << "exception not
 thrown";

If more advanced logic is required, such as handling multiple expected exceptions, use an explicit try block with several catch statements, and report the outcomes using expect(true) or expect(false).

 try
  {
  compute_answer ();
  }
 catch (const std::overflow_error& e)
  {
  mt::expect (true) << "std::overflow_error thrown";
  }
 catch (const std::runtime_error& e)
  {
  mt::expect (true) << "std::runtime_error thrown";
  }
 catch (...)
  {
  mt::expect (false) << "known exception thrown";
  }

Functions

nothrow()

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

Check if a callable does not throw an exception.

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 242 of file micro-test-plus-inlines.h.

throws()

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

Check if a callable throws a specific exception.

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 207 of file micro-test-plus-inlines.h.

throws()

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

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

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 224 of file micro-test-plus-inlines.h.


Generated via docusaurus-plugin-doxygen by Doxygen 1.14.0.