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:
michalsustr 2020-09-06 05:35:53 -06:00 committed by GitHub
parent ce1a07ef45
commit 3bd0d7a8d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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