Replies: 3 comments
-
I then tried
But when I tried it, I got |
Beta Was this translation helpful? Give feedback.
-
I read some more of the docs, that I must have missed the first time, and realized that registering an exception is more complex than what I was doing. The docs don't explain the necessity for every line in the code snippet, such as Here's the relevant part of my code now:
And here's the compile-time error I get: This in Visual Studio 2022, btw. |
Beta Was this translation helpful? Give feedback.
-
So, since I can't figure out how to throw a custom exception, I'm just doing Also, for some reason when I throw that exception, it results in an "unknown exception" or whatever in Python, which is odd, first because it's an exception defined within pybind11, and second because in the docs it's listed as one of the exception that Pybind11 automatically converts to Python exceptions, so I'm not sure if I'm doing something wrong here. |
Beta Was this translation helpful? Give feedback.
-
According to the instructions, register_exception is used like this:
py::register_exception<CppExp>(module, "PyExp");
I'm not sure what's supposed to go in
module
's place. I couldn't find any actual functional examples online.module
is also used as a placeholder in the instructions forPYBIND11_MODULE()
. In that function, I just used an arbitrary name (PyMusic) that I decided my module should be called, without quotes. That part gives me no compilation errors. But using the same name in place ofmodule
inpy::register_exception<CppExp>(PyMusic, "PyExp");
, I get anundeclared identifier
error. I tried it with quotes around 'PyMusic', too, and got a different error in that case.Here's my code:
Second question: Not knowing what to put in place of
module
aside, is this how I should be using it? I just want to throw a custom exception. I couldn't find a way to just directly throw a custom exception, so I'm usingregister_exception
and throwing a C++ exception and hoping it catches it and throws it in Python.Thanks.
Beta Was this translation helpful? Give feedback.
All reactions