Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/data/messages/b/bad-exception-context/bad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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
Comment on lines +1 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

7 changes: 7 additions & 0 deletions doc/data/messages/b/bad-exception-context/good.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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
Comment on lines +1 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

2 changes: 2 additions & 0 deletions doc/data/messages/b/bad-exception-context/related.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `The raise statement <https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement>`_
- `Explicit Exception Chaining <https://peps.python.org/pep-3134/#explicit-exception-chaining>`_ per PEP 3134