‘except’ 子句中没有异常¶
ID: py/useless-except
Kind: problem
Security severity:
Severity: error
Precision: very-high
Tags:
- reliability
- correctness
- types
Query suites:
- python-security-and-quality.qls
如果在 except
处理程序(在 try
语句中)中指定的类不是合法的异常类,那么它将永远不会匹配引发的异常,并且永远不会被执行
合法的异常类是
任何旧式类(仅限 Python 2)
内置类
BaseException
的任何子类 但是,建议您只使用内置类Exception
的子类(它本身是BaseException
的子类)。
建议¶
确保指定的类是预期的类。如果不是,则用正确的类替换它。否则,可以删除整个 except
块。
示例¶
def handle_int():
try:
raise_int()
#This will not cause an exception, but it will be ignored
except int:
print("This will never be printed")