Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creating an Exception hierarchy #3452

Open
stinodego opened this issue Sep 13, 2023 · 1 comment
Open

Allow creating an Exception hierarchy #3452

stinodego opened this issue Sep 13, 2023 · 1 comment

Comments

@stinodego
Copy link

I'd like to be able to create an exception hierarchy in PyO3. In Python I would write something like:

class PolarsError(Exception):
    ...

class OutOfBoundsError(PolarsError):
    ...

Then users could catch the specific error thrown by the code (OutOfBoundsError), or use catch more general exception type (PolarsError).

Right now, I'm using the create_exception! macro to define the exceptions, as mentioned in the user guide. However, this does not allow for specifying a hierarchy.

I've found some discussion on subclassing exceptions:

...from this, it seems like my wish is already possible. However, I cannot find any code examples of how this is supposed to work. If it is already possible, could someone point me in the right direction? If not, consider this a feature request!

@davidhewitt
Copy link
Member

Hello, sorry for the slow reply.

This should definitely be possible and really this just needs proper documenting and testing, after which point I'd like to deprecate the create_exception! macro so we just have one blessed way of doing things.

I've opened #3475 which begins building such a test for an exception hierarchy, let's collaborate there to make sure that all the functionality you want is supported.

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

No branches or pull requests

2 participants