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

Exception constructors should not throw exceptions

    XMLWordPrintable

    Details

    • Type: Bug Detection
    • Status: Deprecated
    • Resolution: Unresolved
    • Labels:
      None
    • Message:
      Remove this "exception".
    • Highlighting:
      Hide

      throw ...

      Show
      throw ...
    • Default Severity:
      Blocker
    • Impact:
      High
    • Likelihood:
      High
    • Covered Languages:
      C#, VB.Net
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      15min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      It may be a good idea to raise an exception in a constructor if you're unable to fully flesh the object in question, but not in an exception constructor. If you do, you'll interfere with the exception that was originally being thrown. Further, it is highly unlikely that an exception raised in the creation of an exception will be properly handled in the calling code, and the unexpected, unhandled exception will lead to program termination.

      Noncompliant Code Example

      class MyException: public std::exception
      {
      public:
        explicit MyException(const char* message):
          msg_(message)
          {
            //...
            if (bad_thing) {
              throw std::exception("A bad thing happened");  // Noncompliant
            }
          }
      

        Attachments

        1.
        C# RSPEC-4166 Language-Specification Active Unassigned
        2.
        VB.NET RSPEC-4893 Language-Specification Active Unassigned

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            ann.campbell.2 Ann Campbell
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated: