-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Examples for bad-exception-context
#6026
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
Examples for bad-exception-context
#6026
Conversation
Co-authored-by: Vladyslav Krylasov <[email protected]>
Pierre-Sassoulas
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick to not use an Exception in the re-raise feel free to ignore.
Co-authored-by: Pierre Sassoulas <[email protected]>
| def divide(x, y): | ||
| result = 0 | ||
| try: | ||
| result = x / y | ||
| except ZeroDivisionError: | ||
| raise ValueError(f"Division by zero when dividing {x} by {y} !") from result # [bad-exception-context] | ||
| return result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def divide(x, y): | |
| result = 0 | |
| try: | |
| result = x / y | |
| except ZeroDivisionError: | |
| raise ValueError(f"Division by zero when dividing {x} by {y} !") from result # [bad-exception-context] | |
| return result | |
| class YParameterIsZero(ZeroDivisionError): | |
| ... | |
| def divide(x, y): | |
| result = 0 | |
| try: | |
| result = x / y | |
| except ZeroDivisionError: | |
| raise YParameterIsZero(f"Division by zero when dividing {x} by {y} !") from result # [bad-exception-context] | |
| return result |
| def divide(x, y): | ||
| result = 0 | ||
| try: | ||
| result = x / y | ||
| except ZeroDivisionError as exc: | ||
| raise ValueError(f"Division by zero when dividing {x} by {y} !") from exc | ||
| return result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| def divide(x, y): | |
| result = 0 | |
| try: | |
| result = x / y | |
| except ZeroDivisionError as exc: | |
| raise ValueError(f"Division by zero when dividing {x} by {y} !") from exc | |
| return result | |
| class YParameterIsZero(ZeroDivisionError): | |
| ... | |
| def divide(x, y): | |
| result = 0 | |
| try: | |
| result = x / y | |
| except ZeroDivisionError as exc: | |
| raise YParameterIsZero(f"Division by zero when dividing {x} by {y} !") from exc | |
| return result |
|
Nevermind the suggestion it's not that important :) |
|
Oops, sorry guys! Little too trigger happy 😅 |
|
😀 No problem, while I think Pierre's last suggestion would have been the best solution, I guess it should be already good enough for the sake of documentation. |
Co-authored-by: Vladyslav Krylasov [email protected]
and preferred name in
script/.contributors_aliases.jsonType of Changes
Description
Examples and related links for
bad-exception-context.Ref: #5953