@@ -53,9 +53,13 @@ exceptions:
53
53
| | a Python exception back to Python. |
54
54
+--------------------------------------+--------------------------------------+
55
55
56
- When a Python function invoked from C++ throws an exception, it is converted
57
- into a C++ exception of type :class: `error_already_set ` whose string payload
58
- contains a textual summary.
56
+ When a Python function invoked from C++ throws an exception, pybind11 will convert
57
+ it into a C++ exception of type :class: `error_already_set ` whose string payload
58
+ contains a textual summary. If you call the Python C-API directly, and it
59
+ returns an error, you should ``throw py::error_already_set(); ``, which allows
60
+ pybind11 will deal with the exception and pass it back to the Python interpreter.
61
+ (You could also use the C-API to clear the error. Either way, the Python error
62
+ must be cleared or Python/pybind11 will be left in an invalid state.)
59
63
60
64
There is also a special exception :class: `cast_error ` that is thrown by
61
65
:func: `handle::call ` when the input arguments cannot be converted to Python
@@ -142,3 +146,50 @@ section.
142
146
Exceptions that you do not plan to handle should simply not be caught, or
143
147
may be explicitly (re-)thrown to delegate it to the other,
144
148
previously-declared existing exception translators.
149
+
150
+ .. _unraisable_exceptions :
151
+
152
+ Handling unraisable exceptions
153
+ ==============================
154
+
155
+ If a Python function invoked from a C++ destructor or any function marked
156
+ ``noexcept(true) `` (collectively, "noexcept functions") throws an exception, there
157
+ is no way to propagate the exception, as such functions may not throw at
158
+ run-time.
159
+
160
+ Neither Python nor C++ allow exceptions raised in a noexcept function to propagate. In
161
+ Python, an exception raised in a class's ``__del__ `` method is logged as an
162
+ unraisable error. In Python 3.8+, a system hook is triggered and an auditing
163
+ event is logged. In C++, ``std::terminate() `` is called to abort immediately.
164
+
165
+ Any noexcept function should have a try-catch block that traps
166
+ class:`error_already_set ` (or any other exception that can occur). Note that pybind11
167
+ wrappers around Python exceptions such as :class: `pybind11::value_error ` are *not *
168
+ Python exceptions; they are C++ exceptions that pybind11 catches and converts to
169
+ Python exceptions. Noexcept function cannot propagate these exceptions either.
170
+ You can convert them to Python exceptions and then discard as unraisable.
171
+
172
+ The following is an elaborate example that catches all possible exception types,
173
+ converting them all to Python exceptions (which is not necessarily what one should
174
+ do).
175
+
176
+ .. code-block :: cpp
177
+
178
+ void nonthrowing_func() noexcept(true) {
179
+ try {
180
+ try {
181
+ ...various functions...
182
+ }
183
+ catch (const py::value_error &e) {
184
+ e.set_error();
185
+ throw py::error_already_set();
186
+ }
187
+ catch (const std::exception &e) {
188
+ PyErr_SetString(e.what());
189
+ throw py::error_already_set();
190
+ }
191
+ }
192
+ catch (py::error_already_set &eas) {
193
+ eas.discard_as_unraisable();
194
+ }
195
+ }
0 commit comments