Catch and swallow exception Medium

Swallowing exceptions, without re-throwing or logging them, is a bad practice. The stack trace, and other useful information for debugging, is lost.

Detector ID
python/swallow-exceptions@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-

Noncompliant example

1def swallow_noncompliant():
2    for i in range(10):
3        try:
4            raise ValueError()
5        finally:
6            # Noncompliant: uses continue or break or return statements
7            # in finally block.
8            continue

Compliant example

1def swallow_compliant():
2    for i in range(10):
3        try:
4            raise ValueError()
5        finally:
6            # Compliant: avoids using continue or break or return statements
7            # in finally block.
8            print("Done with iterations")