mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 05:05:11 +00:00
Add note about specifying custom base class for Exceptions. (#2465)
* Add note about specifying custom base. * Update exception docs based on PR feedback. * Fix trailing whitespace. Co-authored-by: Michal Sustr <michal.sustr@aic.fel.cvut.cz>
This commit is contained in:
parent
ce1a07ef45
commit
3bd0d7a8d5
@ -79,6 +79,19 @@ This call creates a Python exception class with the name ``PyExp`` in the given
|
|||||||
module and automatically converts any encountered exceptions of type ``CppExp``
|
module and automatically converts any encountered exceptions of type ``CppExp``
|
||||||
into Python exceptions of type ``PyExp``.
|
into Python exceptions of type ``PyExp``.
|
||||||
|
|
||||||
|
It is possible to specify base class for the exception using the third
|
||||||
|
parameter, a pointer to `PyObject`:
|
||||||
|
|
||||||
|
.. code-block:: cpp
|
||||||
|
|
||||||
|
py::register_exception<CppExp>(module, "PyExp", PyExc_RuntimeError);
|
||||||
|
|
||||||
|
Then `PyExp` can be caught both as `PyExp` and `RuntimeError`.
|
||||||
|
|
||||||
|
The class objects of the built-in Python exceptions are listed in the Python
|
||||||
|
documentation on `Standard Exceptions <https://docs.python.org/3/c-api/exceptions.html#standard-exceptions>`_.
|
||||||
|
The default base class is `PyExc_Exception`.
|
||||||
|
|
||||||
When more advanced exception translation is needed, the function
|
When more advanced exception translation is needed, the function
|
||||||
``py::register_exception_translator(translator)`` can be used to register
|
``py::register_exception_translator(translator)`` can be used to register
|
||||||
functions that can translate arbitrary exception types (and which may include
|
functions that can translate arbitrary exception types (and which may include
|
||||||
|
Loading…
Reference in New Issue
Block a user