Add support for nested C++11 exceptions (#3608)

* Add support for nested C++11 exceptions

* Remove wrong include

* Fix if directive

* Fix missing skipif

* Simplify code and try to work around MSVC bug

* Clarify comment

* Further simplify code

* Remove the last extra throw statement

* Qualify auto

* Fix typo

* Add missing return for consistency

* Fix clang-tidy complaint

* Fix python2 stub

* Make clang-tidy happy

* Fix compile error

* Fix python2 function signature

* Extract C++20 utility and backport

* Cleanup code a bit more

* Improve test case

* Consolidate code and fix signature

* Fix typo
This commit is contained in:
Aaron Gokaslan
2022-01-14 14:22:47 -05:00
committed by GitHub
parent f8d4aa47b6
commit d2ec836712
4 changed files with 131 additions and 13 deletions

View File

@@ -11,6 +11,8 @@
#include "local_bindings.h"
#include "pybind11_tests.h"
#include <exception>
#include <stdexcept>
#include <utility>
// A type that should be raised as an exception in Python
@@ -105,7 +107,6 @@ struct PythonAlreadySetInDestructor {
py::str s;
};
TEST_SUBMODULE(exceptions, m) {
m.def("throw_std_exception", []() {
throw std::runtime_error("This exception was intentionally thrown.");
@@ -281,5 +282,12 @@ TEST_SUBMODULE(exceptions, m) {
}
});
m.def("throw_nested_exception", []() {
try {
throw std::runtime_error("Inner Exception");
} catch (const std::runtime_error &) {
std::throw_with_nested(std::runtime_error("Outer Exception"));
}
});
#endif
}