Commit 3bd0d7a8 by michalsustr Committed by GitHub

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>
parent ce1a07ef
...@@ -79,6 +79,19 @@ This call creates a Python exception class with the name ``PyExp`` in the given ...@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment