Skip to content

[BUG]: Can't catch the exception in Python that raise in C++ using py::error_already_set() #5290

Closed

Description

@QunBB

Required prerequisites

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

2.10.0

Problem description

When i use the below reproducible example code, I can't catch the exception IndexError in Python that raise in C++.
The log is:

before exception

Process finished with exit code 139 (interrupted by signal 11:SIGSEGV)

Reproducible example code

m.def("test_exception", []() { 
        std::cout << "before exception" << std::endl;

        PyErr_SetString(PyExc_IndexError, "Here is an IndexError");
        throw py::error_already_set();
        
        std::cout << "after exception" << std::endl;
        });
test_exception()

Is this a regression? Put the last known working version here if it is.

Not a regression

Activity

  1. henryiii commented on Jul 25, 2026

    @henryiii
    Collaborator

    🤖 AI text below 🤖

    The reported snippet was compiled against current master, and the IndexError is now caught correctly in Python. The report used 2.10.0; #4298 fixed the cross-DSO exception visibility problem, and that fix is in v2.10.1 and later.

    If you can still reproduce this with current master (v3), please comment and we can reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNew bug, unverified

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions