GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Non-exception in ‘except’ clause — CodeQL query help documentation

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:

  • Any old-style classes (Python 2 only)

  • Any subclass of the builtin class BaseException However, it recommended that you only use subclasses of the builtin class Exception (which is itself a subclass of BaseException).

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¶

  • Python Language Reference: Exceptions.

  • Python Tutorial: Handling Exceptions.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.