Catch and rethrow exception Low

Catching and re-throwing an exception without further actions is redundant and wasteful. Instead, it is recommended to re-throw custom exception type and/or log trace for debugging.

Detector ID
python/catch-and-rethrow-exception@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-

Noncompliant example

1def nested_noncompliant():
2    try:
3        try_something()
4    except KeyError as e:
5        try:
6            catch_and_try_something()
7        # Noncompliant: unnecessary `except` clause.
8        except ValueError:
9            raise
10        raise e

Compliant example

1def nested_compliant():
2    try:
3        try_something()
4    except KeyError as e:
5        try:
6            catch_and_try_something()
7        except ValueError:
8            # Compliant: operation in `except` clause.
9            rethrow_exception_something()
10            raise
11        raise e