Skip to content

Handle C++ crashes gracefully #83

Description

@kwabenantim

Summary

When a wrapped C++ function throws an exception that pybind11 doesn't know how to translate, the exception propagates out of the binding, hits std::terminate, and crashes 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 can't 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.

Metadata

Metadata

Assignees

No one assigned
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions