V667. The 'throw' operator does not possess any arguments and is not situated 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'.

According to Common Weakness Enumeration, potential errors found by using this diagnostic are classified as CWE-480.


Bugs Found

Checked Projects
364
Collected Errors
13 504