16 virtual const char *
what() const noexcept
override {
return message.c_str();}
25 virtual const char *
what() const noexcept
override {
return message.c_str();}
34 virtual const char *
what() const noexcept {
return message.c_str();}
44 virtual const char *
what() const noexcept
override {
return message.c_str();}
69 m.def(
"throw_std_exception", []() {
70 throw std::runtime_error(
"This exception was intentionally thrown.");
77 if (p) std::rethrow_exception(p);
89 if (p) std::rethrow_exception(p);
92 PyErr_SetString(PyExc_RuntimeError, e.
what());
101 if (p) std::rethrow_exception(p);
108 auto ex5 = py::register_exception<MyException5>(m,
"MyException5");
110 py::register_exception<MyException5_1>(m,
"MyException5_1", ex5.ptr());
112 m.def(
"throws1", []() {
throw MyException(
"this error should go to a custom type"); });
113 m.def(
"throws2", []() {
throw MyException2(
"this error should go to a standard Python exception"); });
114 m.def(
"throws3", []() {
throw MyException3(
"this error cannot be translated"); });
115 m.def(
"throws4", []() {
throw MyException4(
"this error is rethrown"); });
116 m.def(
"throws5", []() {
throw MyException5(
"this is a helper-defined translated exception"); });
117 m.def(
"throws5_1", []() {
throw MyException5_1(
"MyException5 subclass"); });
118 m.def(
"throws_logic_error", []() {
throw std::logic_error(
"this error should fall through to the standard handler"); });
119 m.def(
"exception_matches", []() {
126 if (!ex.
matches(PyExc_KeyError))
throw;
131 m.def(
"exception_matches_base", []() {
138 if (!ex.
matches(PyExc_Exception))
throw;
143 m.def(
"modulenotfound_exception_matches_base", []() {
146 py::module::import(
"nonexistent");
149 if (!ex.
matches(PyExc_ImportError))
throw;
155 m.def(
"throw_already_set", [](
bool err) {
157 PyErr_SetString(PyExc_ValueError,
"foo");
160 }
catch (
const std::runtime_error& e) {
161 if ((err && e.what() != std::string(
"ValueError: foo")) ||
162 (!err && e.what() != std::string(
"Unknown internal error occurred")))
165 throw std::runtime_error(
"error message mismatch");
170 PyErr_SetString(PyExc_ValueError,
"foo");
174 m.def(
"python_call_in_destructor", [](
py::dict d) {
177 PyErr_SetString(PyExc_ValueError,
"foo");
virtual const char * what() const noexcept override
MyException5(const std::string &what)
MyException(const char *m)
virtual const char * what() const noexcept override
PythonCallInDestructor(const py::dict &d)
~PythonCallInDestructor()
void register_exception_translator(ExceptionTranslator &&translator)
MyException4(const char *m)
bool matches(handle exc) const
void print(Args &&...args)
#define TEST_SUBMODULE(name, variable)
virtual const char * what() const noexcept override
test_initializer exceptions("exceptions", test_submodule_exceptions)
virtual const char * what() const noexcept
MyException3(const char *m)
MyException2(const char *m)