C and C++


LANG.STRUCT.EXCP.THROW : Use of throw

Summary

The code contains a throw expression.

Properties

Class Name Use of throw
Significance style
Mnemonic LANG.STRUCT.EXCP.THROW
Categories
MisraC++2023 MisraC++2023:18.1.2 An empty throw shall only occur within the compound-statement of a catch handler
CWE CWE:397 Declaration of Throws for Generic Exception
CERT-CPP CERT-CPP:DCL57-CPP Do not let exceptions escape from destructors or deallocation functions
  CERT-CPP:ERR55-CPP Honor exception specifications
  CERT-CPP:ERR58-CPP Handle all exceptions thrown before main() begins executing
  CERT-CPP:ERR61-CPP Catch exceptions by lvalue reference
JSF++ JSF++:208 C++ exceptions shall not be used (i.e. throw, catch and try shall not be used.)
Availability Available for C++ only (not C).
Enabling Checks for this warning class are disabled by default. To enable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += allow class="Use of throw"

Example

#include <stdexcept>

bool has_logic_error(void);

void f_exception_spec(void) throw(std::exception);               // ok: this is an exception specification,
                                                                 // not a throw statement.
void f_throw_stmt(void){
  if (has_logic_error()){
    throw std::logic_error("Logic error."); // 'Use of throw' warning issued here.
  }
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.