pybind / pybind/pybind11

[BUG]: Class attributes are not accessible when caught Python exception using py::register_exception

Open
#5,391 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
What version (or hash if on master) of pybind11 are you using?

2.6.1

Problem description

Hi, I'm encountering an issue when trying to expose custom C++ exceptions to Python using py::register_exception in pybind11. The exception is correctly caught in Python, but the class attributes (e.g., description, errorCode) are not accessible when caught as a Python exception.

Reproducible example code
// Custom C++ exception class with public fields and methods:

namespace Custom {
    class CustomException : public std::exception {
    public:
        std::string description;
        int errorCode;

        CustomException(const std::string& desc, int code)
            : description(desc), errorCode(code) {}

        const char* what() const noexcept override {
            return description.c_str();
        }

        std::string ErrorCodeAsString() const {
            return std::to_string(errorCode);
        }
    };
}

// Bind exception 

void BindCustom(pybind11::module& m) {
    py::register_exception<Custom::CustomException>(m, "CustomExceptionError");

    py::class_<Custom::CustomException>(m, "CustomException")
        .def(py::init<std::string, int>())
        .def("ErrorCodeAsString", &Custom::CustomException::ErrorCodeAsString)
        .def_property("description",
            [](const Custom::CustomException& e) { return e.description; },
            [](Custom::CustomException& e, const std::string& d) { e.description = d; })
        .def_property("errorCode",
            [](const Custom::CustomException& e) { return e.errorCode; },
            [](Custom::CustomException& e, int code) { e.errorCode = code; });

    py::register_exception_translator([](std::exception_ptr p) {
        try {
            if (p) std::rethrow_exception(p);
        } catch (const Custom::CustomException& e) {
            PyErr_SetString(PyExc_RuntimeError, e.what());
        }
    });
}
Is this a regression? Put the last known working version here if it is.

Not a regression

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The report names py::register_exception, py::class_, and the exception translator as the relevant entry points, but it does not identify a source file or test. Start by tracing how the registered exception is created and translated, then verify that caught Python exceptions expose the described attributes and methods without breaking the existing reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.