DAGDA SEARCH | Back to results | Support

Non-exception in ‘except’ clause — CodeQL query help documentation

Source: https://codeql.github.com/codeql-query-help/python/py-useless-except/

Non-exception in ‘except’ clause

ID: py/useless-except
Kind: problem
Security severity: 
Severity: error
Precision: very-high
Tags:
   - quality
   - reliability
   - error-handling
Query suites:
   - python-code-quality.qls
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

If the class specified in an except handler (within a try statement) is not a legal exception class, then it will never match a raised exception and never be executed

Legal exception classes are:

Recommendation

Ensure that the specified class is the one intended. If it is not then replace it with the correct one. Otherwise the entire except block can be deleted.

Example

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")

References