Skip to main content

IAR Embedded Workbench for RISC-V 3.40

THROW-empty (C++ only)

In this section:
Synopsis

Unsafe rethrow of exception.

Enabled by default

No

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

A throw statement without an argument is used outside of a catch handler where there is no exception to rethrow. This is unsafe because a throw statement without an argument rethrows the temporary object that represents the current exception, to allow exception handling to be split over several handlers. This check is identical to MISRAC++2008-15-1-3, MISRAC++2023-18.1.2.

Coding standards
MISRA C++ 2008 15-1-3

(Required) An empty throw (throw;) shall only be used in the compound-statement of a catch handler.

MISRA C++ 2023 18.1.2

(Required) An empty throw shall only occur within the compound-statement of a catch handler

Code examples

The following code example fails the check and will give a warning:

void func()
{
  try
  {
    throw;
  }
  catch (...) {}
}

The following code example passes the check and will not give a warning about this issue:

void func()
{
  try
  {
    throw (42);
  }
  catch (int i)
  {
    if (i > 10)
    {
      throw;
    }
  }
}