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

Classes named like "Exception" should extend "Exception" or a subclass

    XMLWordPrintable

    Details

    • Message:
      Rename this class to remove "(e|E)xception" or correct its inheritance.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      C#, C++, HTML, Objective-C, Python, VB.Net
    • Covered Languages:
      Java, PHP
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources
    • FindBugs:
      NM_CLASS_NOT_EXCEPTION

      Description

      Clear, communicative naming is important in code. It helps maintainers and API users understand the intentions for and uses of a unit of code. Using "exception" in the name of a class that does not extend Exception or one of its subclasses is a clear violation of the expectation that a class' name will indicate what it is and/or does.

      Noncompliant Code Example

      public class FruitException {  // Noncompliant; this has nothing to do with Exception
        private Fruit expected;
        private String unusualCharacteristics;
        private boolean appropriateForCommercialExploitation;
        // ...
      }
      
      public class CarException {  // Noncompliant; the extends clause was forgotten?
        public CarException(String message, Throwable cause) {
        // ...
      

      Compliant Solution

      public class FruitSport {
        private Fruit expected;
        private String unusualCharacteristics;
        private boolean appropriateForCommercialExploitation;
        // ...
      }
      
      public class CarException extends Exception {
        public CarException(String message, Throwable cause) {
        // ...
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              evgeny.mandrikov Evgeny Mandrikov
              Reporter:
              ann.campbell.2 Ann Campbell
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: