Summary
When a wrapped C++ function throws an exception that pybind11 doesn't know how to translate, the exception may propagate out of the binding, hit std::terminate, and crash the whole Python process instead of getting translated into a catchable Python exception. Although pybind11 auto-translates std::exception and its subclasses, exception classes in some C++ code do not derive from std::exception and throw types pybind11 may not be able to handle.
Currently, users can work around this with manual try/except blocks on the Python side. However, it would be good if this didn't have to be done manually.
pybind11 provides a py::register_exception_translator(...) that can be used to catch arbitrary C++ exception types and raise a corresponding Python exception.
Summary
When a wrapped C++ function throws an exception that pybind11 doesn't know how to translate, the exception may propagate out of the binding, hit
std::terminate, and crash the whole Python process instead of getting translated into a catchable Python exception. Although pybind11 auto-translatesstd::exceptionand its subclasses, exception classes in some C++ code do not derive fromstd::exceptionand throw types pybind11 may not be able to handle.Currently, users can work around this with manual try/except blocks on the Python side. However, it would be good if this didn't have to be done manually.
pybind11 provides a py::register_exception_translator(...) that can be used to catch arbitrary C++ exception types and raise a corresponding Python exception.