16 const char *
what() const noexcept
override {
return message.c_str();}
25 const char *
what() const noexcept
override {
return message.c_str();}
34 virtual const char *
what() const noexcept {
return message.c_str();}
44 const char *
what() const noexcept
override {
return message.c_str();}
76 py::object o = foo[
"bar"];
78 catch (py::error_already_set& ex) {
79 ex.discard_as_unraisable(
s);
88 m.def(
"throw_std_exception", []() {
89 throw std::runtime_error(
"This exception was intentionally thrown.");
93 static py::exception<MyException> ex(
m,
"MyException");
96 if (p) std::rethrow_exception(p);
108 if (p) std::rethrow_exception(p);
111 PyErr_SetString(PyExc_RuntimeError, e.
what());
120 if (p) std::rethrow_exception(p);
127 auto ex5 = py::register_exception<MyException5>(
m,
"MyException5");
129 py::register_exception<MyException5_1>(
m,
"MyException5_1", ex5.ptr());
131 m.def(
"throws1", []() {
throw MyException(
"this error should go to a custom type"); });
132 m.def(
"throws2", []() {
throw MyException2(
"this error should go to a standard Python exception"); });
133 m.def(
"throws3", []() {
throw MyException3(
"this error cannot be translated"); });
134 m.def(
"throws4", []() {
throw MyException4(
"this error is rethrown"); });
135 m.def(
"throws5", []() {
throw MyException5(
"this is a helper-defined translated exception"); });
136 m.def(
"throws5_1", []() {
throw MyException5_1(
"MyException5 subclass"); });
137 m.def(
"throws_logic_error", []() {
throw std::logic_error(
"this error should fall through to the standard handler"); });
138 m.def(
"throws_overflow_error", []() {
throw std::overflow_error(
""); });
139 m.def(
"exception_matches", []() {
143 py::object o = foo[
"bar"];
145 catch (py::error_already_set& ex) {
146 if (!ex.matches(PyExc_KeyError))
throw;
151 m.def(
"exception_matches_base", []() {
155 py::object o = foo[
"bar"];
157 catch (py::error_already_set &ex) {
158 if (!ex.matches(PyExc_Exception))
throw;
163 m.def(
"modulenotfound_exception_matches_base", []() {
166 py::module::import(
"nonexistent");
168 catch (py::error_already_set &ex) {
169 if (!ex.matches(PyExc_ImportError))
throw;
175 m.def(
"throw_already_set", [](
bool err) {
177 PyErr_SetString(PyExc_ValueError,
"foo");
179 throw py::error_already_set();
180 }
catch (
const std::runtime_error&
e) {
181 if ((err && e.what() != std::string(
"ValueError: foo")) ||
182 (!err && e.what() != std::string(
"Unknown internal error occurred")))
185 throw std::runtime_error(
"error message mismatch");
190 PyErr_SetString(PyExc_ValueError,
"foo");
191 throw py::error_already_set();
194 m.def(
"python_call_in_destructor", [](py::dict
d) {
197 PyErr_SetString(PyExc_ValueError,
"foo");
198 throw py::error_already_set();
199 }
catch (
const py::error_already_set&) {
205 m.def(
"python_alreadyset_in_destructor", [](
py::str s) {
211 m.def(
"try_catch", [m](py::object exc_type, py::function
f,
py::args args) {
213 catch (py::error_already_set &ex) {
214 if (ex.matches(exc_type))
222 m.def(
"simple_bool_passthrough", [](
bool x) {
return x;});
void print(const Matrix &A, const string &s, ostream &stream)
MyException4(const char *m)
PythonCallInDestructor(const py::dict &d)
~PythonAlreadySetInDestructor()
MyException2(const char *m)
~PythonCallInDestructor()
void foo(CV_QUALIFIER Matrix3d &m)
const char * what() const noexceptoverride
const char * what() const noexceptoverride
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)
const char * what() const noexceptoverride
void register_exception_translator(ExceptionTranslator &&translator)
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