# V667. The 'throw' operator does not have any arguments and is not located within the 'catch' block.

The analyzer has detected that the 'throw' operator doesn't have arguments and is not located inside the 'catch' block. This code may be an error. The 'throw' operator without arguments is used inside the 'catch' block to pass on an exception it has caught to the upper level. According to the standard, a call of the 'throw' operator without an argument will cause the 'std::terminate()' function to be called if the exception is still not caught. It means that the program will be terminated.

Here's an example of incorrect code:

    try
    {
      if (ok)
        return;
      throw;
    }
    catch (...)
    {
    }

We should pass the argument to the 'throw' operator to fix the error.

This is the fixed code:

    try
    {
      if (ok)
        return;
      throw exception("Test");
    }
    catch (...)
    {
    }

However, calling the 'throw' operator outside the 'catch' block is not always an error. For example, if a function is being called from the 'catch' block and serves to pass on the exception to the upper level, no error will occur. But the analyzer may fail to distinguish between the two ways of behavior and will generate the diagnostic message for both. This is an example of such code:

    void error()
    {
      try
      {
        ....
        if (ok)
          return;
        throw; <<== no error here actually
      }
      catch (...)
      {
        throw;
      }
    }

    void foo()
    {
      try
      {
        ....
        if (ok)
          return;
        throw exception("Test");
      }
      catch (...)
      {
        error();
      }
    }

In this case you may suppress the diagnostic message output by adding the comment '//-V667'.

This diagnostic is classified as:

CWE-480 CERT-ERR50-CPP