python - W0703 : Catching too general exception Exception (broad-except)

标签 python pylint

我正在使用 pylint 来检查我的一个 .py 脚本,并显示以下警告:

W0703: Catching too general exception Exception (broad-except)

这是代码的摘录。

try:
    # Execute function1
    function1()
    logger.info('Function 1 successfully executed')
    
except Exception as e:
    send_mail_error(str(e))
    logger.error(str(e))

我想了解 pylint 在这里建议什么,以便改进它。

最佳答案

捕获特定异常是一个很好的做法,请阅读 here .

在您的情况下,您想要记录未知异常并发送电子邮件。在这种情况下,我使用 SMTPHandler 和自定义 sys.excepthook - like this .

关于python - W0703 : Catching too general exception Exception (broad-except),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68485758/

相关文章:

python - 如何禁用 Pylint 警告?

python - 让 pylint 在 pylons/SA 模型中查找继承方法时遇到问题

python - 遵循命名约定从每个文本文件中提取值 - 如何?

python - RDKit 的函数 MolFromInchi 不起作用

python - Xpath获取标签后面的文本

python - 正确使用 utc 时间戳和本地日期时间

python - Pylint UnicodeDecodeError utf-8 无法解码字节

python - 我如何告诉 PyLint "it' 是一个变量,而不是一个常量“来停止消息 C0103?

Python MySQL更新语句不起作用

python - Visual Studio Code Pylint AutoFix 未在 Ubuntu 17.04 上显示