catchorg / catchorg/Catch2

Add support for nested exceptions to REQUIRE_THROWS_AS and REQUIRE_THROWS_WITH

Open
#1,918 3 comments 2 reactions 0 assignees View on GitHub
Feature Request
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

Description

**Description**
When using [`std::throw_with_nested`](https://en.cppreference.com/w/cpp/error/throw_with_nested) it would be nice to easily test that the expected exception chain is generated.

I propose to extend `REQUIRE_THROWS_AS` and `REQUIRE_THROWS_WITH` with `__VA_ARGS__` support facilitating the following code proposal:

```C++
REQUIRE_THROWS_AS(
foo(),
Foo::Exception::IO,
Foo::Exception::OutOfRange,
std::out_of_range
)
```

```C++
REQUIRE_THROWS_WITH(
foo(),
"Failed loading file \"Fooo.bar\"",
"Cannot convert string \"1823417492387419\" to int, valid range is [-2147483648, 2147483647]",
"stoi"
)
```

I further propose that the following checks will fail given that the checks above pass. This is to make sure you do a complete match.

```C++
REQUIRE_THROWS_AS(
foo(),
Foo::Exception::IO,
)
```

```C++
REQUIRE_THROWS_WITH(
foo(),
"Failed loading file \"Fooo.bar\"",
)
```

Maybe add `REQUIRE_THROWS_AS_POSSIBLY_NESTED` and `REQUIRE_THROWS_WITH_POSSIBLY_NESTED` for those that only care about the top level exception, but I will not miss such macros.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.