Search code examples
c++catch2

Catch2 require the exception to be equal to something


I am looking for a replacement for this piece of code, as it gets repeated all over a test segment, where I check if it returns the correct error message (from enum).

try {
  function_that_throws();
} catch (MyErrorEnum error) {
  REQUIRE(error == MyErrorEnum::TheExactError);
}

I think there should be a built-in inside Catch2 for this.


Solution

  • Using classes instead of enums was a great solution by eerorika. Then using REQUIRE_THROW_AS with type instead of value.