Uploaded image for project: 'SonarCFamily'
  1. SonarCFamily
  2. CPP-228

Rule: Exception specifications should not be used

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.5
    • Component/s: Rules
    • Labels:
      None

      Description

      Title

      Exception specifications should not be used

      Description

      <p>
      Specifying which exceptions can be thrown by a function seems like a good idea.
      It can enable the compiler to verify that no other exception is being thrown, and generate more optimal code based on this knowledge.
      </p>
      
      <p>
      However, C++ defers those verification to be done at runtime instead of compile-time.
      When the verification fails, <code>std::unexpected()</code> is called and the program is likely to be terminated.
      </p>
      
      <p>The following code illustrates this rule:</p>
      
      <pre>
      #include &lt;iostream&gt;
      
      void f() throw();   /* Non-Compliant - Declares a function f, with the promise that it will not throw any exception */
      
      void f() throw()    /* Non-Compliant */
      {
        throw 0;          /* But the actual implementation does throw something, yet the code compiles */
      }
      
      int main()
      {
        f();              /* At runtime, the throw is detected, and the program is terminated */
      
        /* Unreachable code */
        std::cout << "Hello, world!" << std::endl;
      
        return 0;
      }
      </pre>
      

      Message

      Remove this exception specification.

      Severity

      Major

      In Sonar way?

      Yes

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              dinesh.bolkensteyn Dinesh Bolkensteyn (Inactive)
              Reporter:
              dinesh.bolkensteyn Dinesh Bolkensteyn (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: