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

Condition-specific "catch" handlers should not be used after the ellipsis (catch-all) handler

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way, MISRA C++ 2008, MISRA C++ 2008 recommended
    • Legacy Key:
      EllipsisHandlerNotLast
    • Covered Languages:
      C++, Objective-C
    • Irrelevant for Languages:
      Python
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      30min

      Description

      The catch-all handler should come last in a chain of catch or @catch statements because it catches everything, and any more-specific catch/@catch that comes after it will never be used, even when the relevant condition occurs.

      This C++ code sample is very similar to the Objective-C equivalent with @try and @catch.

      Noncompliant Code Example

      void f1()
      {
        try
        {
          // ...
        }
        catch (...)
        {
          // Handle all exception types
        }
        catch (std::exception const &e)  // Noncompliant - handler will never be called
        {
        }
      }
      

      Compliant Solution

      void f1()
      {
        try
        {
          // ...
        }
        catch (std::exception const &e)  // Compliant
        {
          // Handle standard exceptions
        }
        catch (...)        // Compliant catch-all handler
        {
          // Handle all other exception types
        }
      }
      

        Attachments

          Issue Links

          1.
          C-Family RSPEC-4358 Language-Specification Active Unassigned

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                Anonymous
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: