Illegal raise¶
ID: py/illegal-raiseKind: problemSecurity severity: Severity: errorPrecision: very-highTags: - quality - reliability - error-handlingQuery suites: - python-security-and-quality.qls
Click to see the query in the CodeQL repository
If the object raised is not a legal Exception class or an instance of one, then aTypeError will be raised instead.
Legal exception classes are:
Any old-style classes (Python 2 only)
Any subclass of the builtin class
BaseExceptionHowever, it recommended that you only use subclasses of the builtin classException(which is itself a subclass ofBaseException).
Recommendation¶
Change the expression in theraise statement to be a legal exception.
Example¶
#Cannot raise an int, even if we want todefraise_int():#Will raise a TypeErrorraise4
References¶
Python Language Reference:Exceptions.
Python Tutorial:Handling Exceptions.