Why this matters
Catching and not re-raising `SystemExit` or `KeyboardInterrupt` prevents Python from exiting properly, leading to unexpected behavior.
Ensure that `SystemExit` and `KeyboardInterrupt` are not caught without being re-raised. Preventing Python from exiting can cause unintended behavior.
Catching and not re-raising `SystemExit` or `KeyboardInterrupt` prevents Python from exiting properly, leading to unexpected behavior.
Side-by-side examples engineers can pattern-match during review.
try:
...
except SystemExit: # Noncompliant: the SystemExit exception is not re-raised.
pass
try:
...
except BaseException: # Noncompliant: BaseExceptions encompass SystemExit exceptions and should be re-raised.
pass
try:
...
except: # Noncompliant: exceptions caught by this statement should be re-raised or a more specific exception should be caught.
passtry:
...
except SystemExit as e:
...
raise e
try:
...
except BaseException as e:
...
raise e
try:
...
except FileNotFoundError:
... # Handle a more specific exceptiontry:
...
except SystemExit: # Noncompliant: the SystemExit exception is not re-raised.
pass
try:
...
except BaseException: # Noncompliant: BaseExceptions encompass SystemExit exceptions and should be re-raised.
pass
try:
...
except: # Noncompliant: exceptions caught by this statement should be re-raised or a more specific exception should be caught.
passtry:
...
except SystemExit as e:
...
raise e
try:
...
except BaseException as e:
...
raise e
try:
...
except FileNotFoundError:
... # Handle a more specific exceptionFrom 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.