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"))
233 != std::string(
"Internal error: pybind11::error_already_set called "
234 "while Python error indicator not set."))) {
236 throw std::runtime_error(
"error message mismatch");
241 PyErr_SetString(PyExc_ValueError,
"foo");
243 throw py::error_already_set();
246 m.def(
"python_call_in_destructor", [](
const py::dict &d) {
250 PyErr_SetString(PyExc_ValueError,
"foo");
251 throw py::error_already_set();
252 }
catch (
const py::error_already_set &) {
258 m.def(
"python_alreadyset_in_destructor", [](
const py::str &s) {
265 [m](
const py::object &exc_type,
const py::function &f,
const py::args &
args) {
268 }
catch (py::error_already_set &ex) {
269 if (ex.matches(exc_type)) {
270 py::print(ex.what());
280 m.def(
"simple_bool_passthrough", [](
bool x) {
return x; });
282 m.def(
"throw_should_be_translated_to_key_error", []() {
throw shared_exception(); });
284 m.def(
"raise_from", []() {
285 PyErr_SetString(PyExc_ValueError,
"inner");
286 py::raise_from(PyExc_ValueError,
"outer");
287 throw py::error_already_set();
290 m.def(
"raise_from_already_set", []() {
292 PyErr_SetString(PyExc_ValueError,
"inner");
293 throw py::error_already_set();
294 }
catch (py::error_already_set &e) {
295 py::raise_from(e, PyExc_ValueError,
"outer");
296 throw py::error_already_set();
300 m.def(
"throw_nested_exception", []() {
302 throw std::runtime_error(
"Inner Exception");
303 }
catch (
const std::runtime_error &) {
304 std::throw_with_nested(std::runtime_error(
"Outer Exception"));
308 m.def(
"error_already_set_what", [](
const py::object &exc_type,
const py::object &exc_value) {
309 PyErr_SetObject(exc_type.ptr(), exc_value.ptr());
310 std::string what = py::error_already_set().what();
311 bool py_err_set_after_what = (PyErr_Occurred() !=
nullptr);
313 return py::make_tuple(std::move(what), py_err_set_after_what);
316 m.def(
"test_cross_module_interleaved_error_already_set", []() {
317 auto cm = py::module_::import(
"cross_module_interleaved_error_already_set");
318 auto interleaved_error_already_set
319 =
reinterpret_cast<void (*)()
>(PyLong_AsVoidPtr(cm.attr(
"funcaddr").ptr()));
320 interleaved_error_already_set();
323 m.def(
"test_error_already_set_double_restore", [](
bool dry_run) {
324 PyErr_SetString(PyExc_ValueError,
"Random error.");
325 py::error_already_set e;
334 m.def(
"test_pypy_oserror_normalization", []() {
336 py::module_::import(
"io").attr(
"open")(
"this_filename_must_not_exist",
"r");
337 }
catch (
const py::error_already_set &e) {
338 return py::str(e.what());
340 return py::str(
"UNEXPECTED");
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)