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 PyErr_SetObject(exc_type.ptr(), exc_value.ptr());
110 return py::error_already_set().what();
114 m.def(
"throw_std_exception",
115 []() {
throw std::runtime_error(
"This exception was intentionally thrown."); });
118 static py::exception<MyException> ex(
m,
"MyException");
122 std::rethrow_exception(p);
136 std::rethrow_exception(p);
140 PyErr_SetString(PyExc_RuntimeError, e.
what());
150 std::rethrow_exception(p);
158 auto ex5 = py::register_exception<MyException5>(
m,
"MyException5");
160 py::register_exception<MyException5_1>(
m,
"MyException5_1", ex5.ptr());
167 std::rethrow_exception(p);
170 PyErr_SetString(PyExc_RuntimeError, e.
what());
174 m.def(
"throws1", []() {
throw MyException(
"this error should go to a custom type"); });
176 []() {
throw MyException2(
"this error should go to a standard Python exception"); });
177 m.def(
"throws3", []() {
throw MyException3(
"this error cannot be translated"); });
178 m.def(
"throws4", []() {
throw MyException4(
"this error is rethrown"); });
180 []() {
throw MyException5(
"this is a helper-defined translated exception"); });
181 m.def(
"throws5_1", []() {
throw MyException5_1(
"MyException5 subclass"); });
182 m.def(
"throws6", []() {
throw MyException6(
"MyException6 only handled in this module"); });
183 m.def(
"throws_logic_error", []() {
184 throw std::logic_error(
"this error should fall through to the standard handler");
186 m.def(
"throws_overflow_error", []() {
throw std::overflow_error(
""); });
187 m.def(
"throws_local_error", []() {
throw LocalException(
"never caught"); });
189 m.def(
"exception_matches", []() {
193 py::object o = foo[
"bar"];
194 }
catch (py::error_already_set &ex) {
195 if (!ex.matches(PyExc_KeyError)) {
202 m.def(
"exception_matches_base", []() {
206 py::object o = foo[
"bar"];
207 }
catch (py::error_already_set &ex) {
208 if (!ex.matches(PyExc_Exception)) {
215 m.def(
"modulenotfound_exception_matches_base", []() {
218 py::module_::import(
"nonexistent");
219 }
catch (py::error_already_set &ex) {
220 if (!ex.matches(PyExc_ImportError)) {
228 m.def(
"throw_already_set", [](
bool err) {
230 PyErr_SetString(PyExc_ValueError,
"foo");
233 throw py::error_already_set();
234 }
catch (
const std::runtime_error &
e) {
235 if ((err && e.what() != std::string(
"ValueError: foo"))
238 != std::string(
"Internal error: pybind11::error_already_set called " 239 "while Python error indicator not set."))) {
241 throw std::runtime_error(
"error message mismatch");
246 PyErr_SetString(PyExc_ValueError,
"foo");
248 throw py::error_already_set();
251 m.def(
"python_call_in_destructor", [](
const py::dict &
d) {
255 PyErr_SetString(PyExc_ValueError,
"foo");
256 throw py::error_already_set();
257 }
catch (
const py::error_already_set &) {
263 m.def(
"python_alreadyset_in_destructor", [](
const py::str &
s) {
270 [m](
const py::object &exc_type,
const py::function &
f,
const py::args &
args) {
273 }
catch (py::error_already_set &ex) {
274 if (ex.matches(exc_type)) {
285 m.def(
"simple_bool_passthrough", [](
bool x) {
return x; });
287 m.def(
"throw_should_be_translated_to_key_error", []() {
throw shared_exception(); });
289 m.def(
"raise_from", []() {
290 PyErr_SetString(PyExc_ValueError,
"inner");
292 throw py::error_already_set();
295 m.def(
"raise_from_already_set", []() {
297 PyErr_SetString(PyExc_ValueError,
"inner");
298 throw py::error_already_set();
299 }
catch (py::error_already_set &
e) {
301 throw py::error_already_set();
305 m.def(
"throw_nested_exception", []() {
307 throw std::runtime_error(
"Inner Exception");
308 }
catch (
const std::runtime_error &) {
309 std::throw_with_nested(std::runtime_error(
"Outer Exception"));
313 m.def(
"error_already_set_what", [](
const py::object &exc_type,
const py::object &exc_value) {
314 PyErr_SetObject(exc_type.ptr(), exc_value.ptr());
315 std::string
what = py::error_already_set().what();
316 bool py_err_set_after_what = (PyErr_Occurred() !=
nullptr);
321 m.def(
"test_cross_module_interleaved_error_already_set", []() {
322 auto cm = py::module_::import(
"cross_module_interleaved_error_already_set");
323 auto interleaved_error_already_set
324 =
reinterpret_cast<void (*)()
>(PyLong_AsVoidPtr(
cm.attr(
"funcaddr").ptr()));
325 interleaved_error_already_set();
328 m.def(
"test_error_already_set_double_restore", [](
bool dry_run) {
329 PyErr_SetString(PyExc_ValueError,
"Random error.");
330 py::error_already_set
e;
MyException4(const char *m)
PythonCallInDestructor(const py::dict &d)
const char * what() const noexcept override
~PythonAlreadySetInDestructor()
void raise_from(PyObject *type, const char *message)
MyException2(const char *m)
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
void register_exception_translator(ExceptionTranslator &&translator)
~PythonCallInDestructor()
MyException6(const char *m)
void foo(CV_QUALIFIER Matrix3d &m)
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
MyException(const char *m)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
MyException3(const char *m)
TEST_SUBMODULE(exceptions, m)
std::string error_already_set_what(const py::object &exc_type, const py::object &exc_value)
const char * what() const noexcept override
const char * what() const noexcept override
PythonAlreadySetInDestructor(const py::str &s)
MyException5(const std::string &what)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
virtual const char * what() const noexcept
void register_local_exception_translator(ExceptionTranslator &&translator)
const char * what() const noexcept override