From 3bd0d7a8d58afed91a4aacf5d0cdb8443e107825 Mon Sep 17 00:00:00 2001 From: michalsustr Date: Sun, 6 Sep 2020 05:35:53 -0600 Subject: [PATCH] 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 --- docs/advanced/exceptions.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/advanced/exceptions.rst b/docs/advanced/exceptions.rst index b7d36014a..874af4cc4 100644 --- a/docs/advanced/exceptions.rst +++ b/docs/advanced/exceptions.rst @@ -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`` 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(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 `_. +The default base class is `PyExc_Exception`. + When more advanced exception translation is needed, the function ``py::register_exception_translator(translator)`` can be used to register functions that can translate arbitrary exception types (and which may include