非法 raise¶
ID: py/illegal-raise
Kind: problem
Security severity:
Severity: error
Precision: very-high
Tags:
- reliability
- correctness
- types
Query suites:
- python-security-and-quality.qls
如果引发的对象不是合法的异常类或其实例,则会引发 TypeError
。
合法的异常类包括
任何旧式类(仅限 Python 2)
任何内置类
BaseException
的子类。但是,建议您仅使用内置类Exception
的子类(它本身是BaseException
的子类)。
建议¶
将 raise
语句中的表达式更改为合法异常。
示例¶
#Cannot raise an int, even if we want to
def raise_int():
#Will raise a TypeError
raise 4