Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-5713

A subclass should not be in the same "except" statement as a parent class

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Hide
      * Remove this duplicate Exception class.
      * Remove this useless Exception class; it derives from class XXX which is already caught.
      Show
      * Remove this duplicate Exception class. * Remove this useless Exception class; it derives from class XXX which is already caught.
    • Highlighting:
      Hide

      If the class is a duplicate:

      • Primary:
        • The first time the Exception class appears in the "except" statement.
      • Secondary:
        • All the duplicates
          message: 'Duplicate.'

      If the class inherit from another class in the same "except" statement:

      • Primary:
        • The derived class.
      • Secondary:
        • The parent class.
          message: 'Parent class.'
      Show
      If the class is a duplicate: Primary: The first time the Exception class appears in the "except" statement. Secondary: All the duplicates message: 'Duplicate.' If the class inherit from another class in the same "except" statement: Primary: The derived class. Secondary: The parent class. message: 'Parent class.'
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      PHP, Python
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      1min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      Repeating an exception class in a single except statement will not fail but it is not what the developer intended. Either the class is not the one which should be caught, or this is dead code.

      Having a subclass and a parent class in the same except statement is also useless. It is enough to keep only the parent class.

      This rule raises an issue when an exception class is duplicated in an except statement, or when an exception class has a parent class in the same except statement.

      Noncompliant Code Example

      try:
          raise NotImplementedError()
      except (NotImplementedError, RuntimeError):  # Noncompliant. NotImplementedError inherits from RuntimeError
          print("Foo")
      
      try:
          raise NotImplementedError()
      except (RuntimeError, RuntimeError):  # Noncompliant.
          print("Foo")
      

      Compliant Solution

      try:
          raise NotImplementedError()
      except RuntimeError:
          print("Foo")
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              nicolas.harraudeau Nicolas Harraudeau (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: