22 const char *
what() const noexcept
override {
return message.c_str(); }
32 const char *
what() const noexcept
override {
return message.c_str(); }
42 virtual const char *
what() const noexcept {
return message.c_str(); }
59 const char *
what() const noexcept
override {
return message.c_str(); }
80 const char *
what() const noexcept
override {
return message.c_str(); }
99 py::object o = foo[
"bar"];
100 }
catch (py::error_already_set &ex) {
101 ex.discard_as_unraisable(
s);
109 m.def(
"throw_std_exception",
110 []() {
throw std::runtime_error(
"This exception was intentionally thrown."); });
113 static py::exception<MyException> ex(m,
"MyException");
114 py::register_exception_translator([](std::exception_ptr p) {
117 std::rethrow_exception(p);
128 py::register_exception_translator([](std::exception_ptr p) {
131 std::rethrow_exception(p);
135 PyErr_SetString(PyExc_RuntimeError, e.
what());
142 py::register_exception_translator([](std::exception_ptr p) {
145 std::rethrow_exception(p);
153 auto ex5 = py::register_exception<MyException5>(m,
"MyException5");
155 py::register_exception<MyException5_1>(m,
"MyException5_1", ex5.ptr());
159 py::register_local_exception_translator([](std::exception_ptr p) {
162 std::rethrow_exception(p);
165 PyErr_SetString(PyExc_RuntimeError, e.
what());
169 m.def(
"throws1", []() {
throw MyException(
"this error should go to a custom type"); });
171 []() {
throw MyException2(
"this error should go to a standard Python exception"); });
172 m.def(
"throws3", []() {
throw MyException3(
"this error cannot be translated"); });
173 m.def(
"throws4", []() {
throw MyException4(
"this error is rethrown"); });
175 []() {
throw MyException5(
"this is a helper-defined translated exception"); });
176 m.def(
"throws5_1", []() {
throw MyException5_1(
"MyException5 subclass"); });
177 m.def(
"throws6", []() {
throw MyException6(
"MyException6 only handled in this module"); });
178 m.def(
"throws_logic_error", []() {
179 throw std::logic_error(
"this error should fall through to the standard handler");
181 m.def(
"throws_overflow_error", []() {
throw std::overflow_error(
""); });
182 m.def(
"throws_local_error", []() {
throw LocalException(
"never caught"); });
184 m.def(
"exception_matches", []() {
188 py::object o = foo[
"bar"];
189 }
catch (py::error_already_set &ex) {
190 if (!ex.matches(PyExc_KeyError)) {
197 m.def(
"exception_matches_base", []() {
201 py::object o = foo[
"bar"];
202 }
catch (py::error_already_set &ex) {
203 if (!ex.matches(PyExc_Exception)) {
210 m.def(
"modulenotfound_exception_matches_base", []() {
213 py::module_::import(
"nonexistent");
214 }
catch (py::error_already_set &ex) {
215 if (!ex.matches(PyExc_ImportError)) {
223 m.def(
"throw_already_set", [](
bool err) {
225 PyErr_SetString(PyExc_ValueError,
"foo");
228 throw py::error_already_set();
229 }
catch (
const std::runtime_error &e) {
230 if ((err && e.what() != std::string(
"ValueError: foo"))
231 || (!err && e.what() != std::string(
"Unknown internal error occurred"))) {
233 throw std::runtime_error(
"error message mismatch");
238 PyErr_SetString(PyExc_ValueError,
"foo");
240 throw py::error_already_set();
243 m.def(
"python_call_in_destructor", [](
const py::dict &d) {
247 PyErr_SetString(PyExc_ValueError,
"foo");
248 throw py::error_already_set();
249 }
catch (
const py::error_already_set &) {
255 m.def(
"python_alreadyset_in_destructor", [](
const py::str &s) {
262 [m](
const py::object &exc_type,
const py::function &f,
const py::args &
args) {
265 }
catch (py::error_already_set &ex) {
266 if (ex.matches(exc_type)) {
267 py::print(ex.what());
275 m.def(
"simple_bool_passthrough", [](
bool x) {
return x; });
277 m.def(
"throw_should_be_translated_to_key_error", []() {
throw shared_exception(); });
279#if PY_VERSION_HEX >= 0x03030000
281 m.def(
"raise_from", []() {
282 PyErr_SetString(PyExc_ValueError,
"inner");
283 py::raise_from(PyExc_ValueError,
"outer");
284 throw py::error_already_set();
287 m.def(
"raise_from_already_set", []() {
289 PyErr_SetString(PyExc_ValueError,
"inner");
290 throw py::error_already_set();
291 }
catch (py::error_already_set &e) {
292 py::raise_from(e, PyExc_ValueError,
"outer");
293 throw py::error_already_set();
297 m.def(
"throw_nested_exception", []() {
299 throw std::runtime_error(
"Inner Exception");
300 }
catch (
const std::runtime_error &) {
301 std::throw_with_nested(std::runtime_error(
"Outer Exception"));
const char * what() const noexcept override
MyException2(const char *m)
virtual ~MyException3()=default
MyException3(const char *m)
MyException3 & operator=(MyException3 &&)=default
MyException3 & operator=(const MyException3 &)=default
MyException3(const MyException3 &)=default
virtual const char * what() const noexcept
MyException3(MyException3 &&)=default
MyException4(const char *m)
const char * what() const noexcept override
MyException5(const std::string &what)
const char * what() const noexcept override
MyException6(const char *m)
const char * what() const noexcept override
MyException(const char *m)
#define TEST_SUBMODULE(name, variable)
~PythonAlreadySetInDestructor()
PythonAlreadySetInDestructor(const py::str &s)
~PythonCallInDestructor()
PythonCallInDestructor(const py::dict &d)