Skip to content
Discussion options

You must be logged in to vote

Ok, I think I've found a way but without using the create_exception! macro.
This is the custom exception definition:

use pyo3::exceptions::PyException;
use pyo3::prelude::*;

#[pyclass(extends=PyException)]
pub struct SomethingWentWrongException {
    #[pyo3(get, set)]
    pub data: String,
}

#[pymethods]
impl SomethingWentWrongException {
    #[new]
    fn new(data: &str) -> Self {
        SomethingWentWrongException { data: data.to_string() }
    }
}

And this how I'm creating a new instance of it:

let exception = PyErr::new::<SomethingWentWrongException, _>(("Custom data string here").to_object(py));
Err(exception)

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by faxioman
Comment options

You must be logged in to vote
1 reply
@davidhewitt
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants