Python

Python

Made by DeepSource

Catching previously caught exception PYL-W0705

Bug risk
Critical
Autofix

An exception should be caught only once in a try-except block. It is useless to catch the previously caught exception later in other else blocks as the previous exception block handling the exception would be executed every time.

Not preferred

try:
    1 / 0
except ZeroDivisionError:
    print("Divided by zero")
except (OSError, ZeroDivisionError):
    print("Handles an exception which was caught before.")

Preferred

try:
    1 / 0
except ZeroDivisionError:
    print("Divided by zero")
except OSError:
    print("Handles a unique exception")