'除了异常:'的pylint警告 [英] pylint warning on 'except Exception:'

查看:63
本文介绍了'除了异常:'的pylint警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这样的块:

try:
    #some stuff
except Exception:
    pass

pylint 引发警告 W0703 'Catch "Exception"'.为什么?

pylint raises warning W0703 'Catch "Exception"'. Why?

推荐答案

通常不捕获根 Exception 对象,而不是捕获更具体的对象(例如 IOException)被认为是一种很好的做法.

It's considered good practice to not normally catch the root Exception object, instead of catching more specific ones - for example IOException.

考虑是否发生内存不足异常 - 仅使用pass"不会让您的程序处于良好状态.

Consider if an out of memory exception occurred - simply using "pass" isn't going to leave your programme in a good state.

几乎唯一应该在程序的顶层捕获异常的时间,您可以(尝试)记录它,显示错误,然后尽可能优雅地退出.

Pretty much the only time you should catch Exception is at the top level of your programme, where you can (try to) log it, display an error, and exit as gracefully as you can.

这篇关于'除了异常:'的pylint警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆