Skip to content

Commit 1cd9965

Browse files
committed
List all pybind11 exceptions and refer to code.
I wanted to know excacly what existed, because I wanted to raise a `RuntimeError`. In practice, it took me quite some time to find where (a) the pybind11 exceptions are defined, (b) how the C++ exceptions are converted to Python ones. At the end, I discovered everything else is converted to RuntimeError, so I do not need to do anything. The link is a result to github search, which may be more robust to moving code around.
1 parent 17c22b9 commit 1cd9965

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

docs/advanced/exceptions.rst

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ that will trap C++ exceptions, translate them to the corresponding Python except
99
and raise them so that Python code can handle them.
1010

1111
pybind11 defines translations for ``std::exception`` and its standard
12-
subclasses, and several special exception classes that translate to specific
13-
Python exceptions. Note that these are not actually Python exceptions, so they
12+
subclasses (see [translate_exception]_) and several special exception classes
13+
that translate to specific Python exceptions (see [PYBIND11_RUNTIME_EXCEPTION]_).
14+
Note that these are not actually Python exceptions, so they
1415
cannot be examined using the Python C API. Instead, they are pure C++ objects
1516
that pybind11 will translate the corresponding Python exception when they arrive
1617
at its exception handler.
1718

19+
.. [translate_exception] https://github.com/pybind/pybind11/search?q=translate_exception
20+
.. [PYBIND11_RUNTIME_EXCEPTION] https://github.com/pybind/pybind11/blob/master/include/pybind11/detail/common.h
21+
1822
.. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}|
1923

2024
+--------------------------------------+--------------------------------------+
@@ -43,15 +47,23 @@ at its exception handler.
4347
| | of bounds access in ``__getitem__``, |
4448
| | ``__setitem__``, etc.) |
4549
+--------------------------------------+--------------------------------------+
46-
| :class:`pybind11::value_error` | ``ValueError`` (used to indicate |
47-
| | wrong value passed in |
48-
| | ``container.remove(...)``) |
49-
+--------------------------------------+--------------------------------------+
5050
| :class:`pybind11::key_error` | ``KeyError`` (used to indicate out |
5151
| | of bounds access in ``__getitem__``, |
5252
| | ``__setitem__`` in dict-like |
5353
| | objects, etc.) |
5454
+--------------------------------------+--------------------------------------+
55+
| :class:`pybind11::value_error` | ``ValueError`` (used to indicate |
56+
| | wrong value passed in |
57+
| | ``container.remove(...)``) |
58+
+--------------------------------------+--------------------------------------+
59+
| :class:`pybind11::type_error` | ``TypeError`` |
60+
+--------------------------------------+--------------------------------------+
61+
| :class:`pybind11::buffer_error` | ``BufferError`` |
62+
+--------------------------------------+--------------------------------------+
63+
| :class:`pybind11::import_error` | ``import_error` |
64+
+--------------------------------------+--------------------------------------+
65+
| Any other exception | ``RuntimeError`` |
66+
+--------------------------------------+--------------------------------------+
5567

5668
Exception translation is not bidirectional. That is, *catching* the C++
5769
exceptions defined above above will not trap exceptions that originate from

0 commit comments

Comments
 (0)