Why this matters
Large `try` blocks make it harder to pinpoint what caused an exception. Keeping them small ensures that only the necessary code is monitored for errors.
Detect `try` blocks that contain excessive code. Large `try` blocks make it harder to debug errors. Recommend narrowing the scope of the `try` block to only the necessary code.
Large `try` blocks make it harder to pinpoint what caused an exception. Keeping them small ensures that only the necessary code is monitored for errors.
Side-by-side examples engineers can pattern-match during review.
try:
x = 10
y = x / 0
print(y)
except ZeroDivisionError:
print('Error')x = 10
try:
y = x / 0
except ZeroDivisionError:
print('Error')try:
x = 10
y = x / 0
print(y)
except ZeroDivisionError:
print('Error')x = 10
try:
y = x / 0
except ZeroDivisionError:
print('Error')From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.