Non-exception in ‘except’ clause¶
ID: py/useless-exceptKind: 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 class specified in anexcept handler (within atry statement) is not a legal exception class, then it will never match a raised exception and never be executed
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¶
Ensure that the specified class is the one intended. If it is not then replace it with the correct one. Otherwise the entireexcept block can be deleted.
Example¶
defhandle_int():try:raise_int()#This will not cause an exception, but it will be ignoredexceptint:print("This will never be printed")
References¶
Python Language Reference:Exceptions.
Python Tutorial:Handling Exceptions.