跳到内容

try-except-continue (S112)

源自 flake8-bandit linter。

作用

检查 try-except-continue 模式的使用。

为什么这不好?

try-except-continue 模式会抑制所有异常。 抑制异常可能会隐藏错误,否则这些错误可能会揭示意外行为、安全漏洞或恶意活动。 相反,请考虑记录异常。

示例

import logging

while predicate:
    try:
        ...
    except Exception:
        continue

建议改为

import logging

while predicate:
    try:
        ...
    except Exception as exc:
        logging.exception("Error occurred")

Options (选项)

参考