16 const char *
what() const noexcept
override {
return message.c_str();}
18 std::string message =
"";
25 const char *
what() const noexcept
override {
return message.c_str();}
27 std::string message =
"";
34 virtual const char *
what() const noexcept {
return message.c_str();}
43 std::string message =
"";
51 const char *
what() const noexcept
override {
return message.c_str();}
53 std::string message =
"";
60 explicit MyException5(
const std::string &what) : std::logic_error(what) {}
83 py::object o = foo[
"bar"];
85 catch (py::error_already_set& ex) {
86 ex.discard_as_unraisable(
s);
95 m.def(
"throw_std_exception", []() {
96 throw std::runtime_error(
"This exception was intentionally thrown.");
100 static py::exception<MyException> ex(
m,
"MyException");
103 if (p) std::rethrow_exception(p);
115 if (p) std::rethrow_exception(p);
118 PyErr_SetString(PyExc_RuntimeError, e.
what());
127 if (p) std::rethrow_exception(p);
134 auto ex5 = py::register_exception<MyException5>(
m,
"MyException5");
136 py::register_exception<MyException5_1>(
m,
"MyException5_1", ex5.ptr());
138 m.def(
"throws1", []() {
throw MyException(
"this error should go to a custom type"); });
139 m.def(
"throws2", []() {
throw MyException2(
"this error should go to a standard Python exception"); });
140 m.def(
"throws3", []() {
throw MyException3(
"this error cannot be translated"); });
141 m.def(
"throws4", []() {
throw MyException4(
"this error is rethrown"); });
142 m.def(
"throws5", []() {
throw MyException5(
"this is a helper-defined translated exception"); });
143 m.def(
"throws5_1", []() {
throw MyException5_1(
"MyException5 subclass"); });
144 m.def(
"throws_logic_error", []() {
throw std::logic_error(
"this error should fall through to the standard handler"); });
145 m.def(
"throws_overflow_error", []() {
throw std::overflow_error(
""); });
146 m.def(
"exception_matches", []() {
150 py::object o = foo[
"bar"];
152 catch (py::error_already_set& ex) {
153 if (!ex.matches(PyExc_KeyError))
throw;
158 m.def(
"exception_matches_base", []() {
162 py::object o = foo[
"bar"];
164 catch (py::error_already_set &ex) {
165 if (!ex.matches(PyExc_Exception))
throw;
170 m.def(
"modulenotfound_exception_matches_base", []() {
173 py::module_::import(
"nonexistent");
175 catch (py::error_already_set &ex) {
176 if (!ex.matches(PyExc_ImportError))
throw;
182 m.def(
"throw_already_set", [](
bool err) {
184 PyErr_SetString(PyExc_ValueError,
"foo");
186 throw py::error_already_set();
187 }
catch (
const std::runtime_error& e) {
188 if ((err && e.what() != std::string(
"ValueError: foo")) ||
189 (!err && e.what() != std::string(
"Unknown internal error occurred")))
192 throw std::runtime_error(
"error message mismatch");
197 PyErr_SetString(PyExc_ValueError,
"foo");
198 throw py::error_already_set();
201 m.def(
"python_call_in_destructor", [](py::dict d) {
204 PyErr_SetString(PyExc_ValueError,
"foo");
205 throw py::error_already_set();
206 }
catch (
const py::error_already_set&) {
212 m.def(
"python_alreadyset_in_destructor", [](py::str s) {
218 m.def(
"try_catch", [
m](py::object exc_type, py::function f, py::args
args) {
220 catch (py::error_already_set &ex) {
221 if (ex.matches(exc_type))
229 m.def(
"simple_bool_passthrough", [](
bool x) {
return x;});