Main Content

AUTOSAR C++14 Rule A15-4-4

A declaration of non-throwing function shall contain noexcept specification

Since R2021a

Description

Rule Definition

A declaration of non-throwing function shall contain noexcept specification.

Rationale

Specifying functions that do not raise exceptions by using the specifier noexcept or noexcept(true) enables the compiler to perform certain optimizations for these functions, such as omitting the exception handling process. Specifying the exception specification of functions clearly communicates that you expect the functions to not raise exceptions.

Specify functions that do not raise exceptions by using the specifier noexcept. If the exception specification of a function depends on a template argument, use noexcept(<condition>). If the exception specification of a function is unknown, assume it raises exceptions.

Polyspace Implementation

Polyspace® flags the definition of a callable entity, such as function, class or function template, or class constructors if the following is true:

  • The callable entity is defined. Polyspace does not flag functions that are declared but not defined. Polyspace checks function or class templates that have at least one instantiation.

  • The callable entity raises no exceptions. In case of templates of classes and functions, at least one instantiation raises no exception. When checking callable entities for exceptions, Polyspace assumes external functions with no definitions behave as noexcept(true). For more information about how Polyspace checks if a callable entity raises an exception, see the Polyspace Implementation section of AUTOSAR C++14 Rule A15-4-2.

  • The callable entity has no exception specification.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <iostream>
#include <stdexcept>
void F1();  
void F2() noexcept; 
void F3() noexcept(true); 
void F4() noexcept(false); 

void F5(){ //Noncompliant

	F2();    
	F3();    

}

void F6() noexcept    // Compliant
{
	try {
		F4();   
	}catch (std::exception& e) {
		// Handle exceptions
	}
}

class NotThrowing {
public:
	NotThrowing() { // Noncompliant
		F2();
	}
};

class Throwing{
public:
	Throwing() { // Compliant 
		F4();
	}
};
template <class T, bool B> void CompliantClasshandler() noexcept(B) { //Compliant
	//...
}

template <class T> void NoncompliantClasshandler() { // Noncompliant
	//...
}

int Factory() noexcept{
	Throwing a;
	NotThrowing b;
	NoncompliantClasshandler<Throwing>();
	NoncompliantClasshandler<NotThrowing>();
	CompliantClasshandler<Throwing, true>();
	CompliantClasshandler<NotThrowing, false>();
	return 1;
}

  • The function F5 does not raise exceptions but it is not marked as noexcept in the code. Polyspace flags the definitions of the function. The function F6 does not raise exceptions and it is specified as noexcept in the code. Polyspace does not flag F6.

  • Polyspace checks constructors of classes when the classes are used in the code. In this example, the classes Throwing and NotThrowing are instantiated and Polyspace checks their constructors.

    The constructor of the class NotThrowing does not raise exceptions but it is not specified as noexcept in the code. Polyspace flags the function. The constructor of the class Throwing raises exception and it is not specified as noexcept in the code. Polyspace does not flag this constructor.

  • Polyspace checks function templates when the template is instantiated. In this example, Polyspace checks the templates CompliantClasshandler and NoncompliantClasshandler because they are instantiated in Factory.

    Because the instantiation of NoncompliantClasshandler in Factory with the class NotThrowing does not raise exception, and the template is not specified as noexcept in the code, Polyspace flags the definition of NoncompliantClasshandler. Polyspace does not flag the template CompliantClasshandler because it is specified by a conditionalized noexcept operator in the code.

Check Information

Group: Exception handling
Category: Required, Automated

Version History

Introduced in R2021a

expand all