Skip to content

Commit e41b2f0

Browse files
DudeNr33sprytnykPierre-Sassoulas
authored
Examples for bad-exception-context (#6026)
Co-authored-by: Vladyslav Krylasov <[email protected]> Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 0227e7f commit e41b2f0

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def divide(x, y):
2+
result = 0
3+
try:
4+
result = x / y
5+
except ZeroDivisionError:
6+
raise ValueError(f"Division by zero when dividing {x} by {y} !") from result # [bad-exception-context]
7+
return result
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def divide(x, y):
2+
result = 0
3+
try:
4+
result = x / y
5+
except ZeroDivisionError as exc:
6+
raise ValueError(f"Division by zero when dividing {x} by {y} !") from exc
7+
return result
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `The raise statement <https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement>`_
2+
- `Explicit Exception Chaining <https://peps.python.org/pep-3134/#explicit-exception-chaining>`_ per PEP 3134

0 commit comments

Comments
 (0)