Do not use bare except


Python Clean Code Tip:

Avoid empty except blocks -> try-except-pass.

They lead to hard-to-find bugs.

👇

# bad
import logging


def send_email():
    print("Sending email")
    raise ConnectionError("Oops")


try:
    send_email()
except:  # AVOID THIS
    pass


# better
logger = logging.getLogger(__name__)
try:
    send_email()
except ConnectionError as exc:
    logger.error(f"Cannot send email {exc}")