Main Content

AUTOSAR C++14 Rule A15-0-3

Exception safety guarantee of a called function shall be considered

Since R2022a

Description

Rule Definition

Exception safety guarantee of a called function shall be considered.

Rationale

The exception safety level of a function affects the function behavior. Functions that have a basic exception safety do not modify the class invariant when they exit with an exception. Functions that have a strong exception safety restores the class invariant to the state it was before the function call.

If a function provides neither of these exception safety, calling it might lead to unexpected behavior. For instance, an external function might exit with an exception for a specific input. When a client uses this function, the exception might be unexpected, resulting in a memory leak or program termination.

Polyspace Implementation

The emplace() method uses constructors of objects to construct them in-place within the containers. An exception arising from this method leaves the constructor in an invalid state. If the called constructors contain throw() statements, or calls a function that raises exception, Polyspace® flags the emplace() method.

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<exception>
#include<vector>

class ratio{
public:
	ratio(double in1=1, double in2=1):num{in1}, denom{in2}{
		if(in2==0)
		throw std::invalid_argument("Denominator cannot be zero");
		
	}
private:
	double num;
	double denom;
	
};

void foo(){
	std::vector<ratio> v;
	v.reserve(10);
	std::vector<ratio>::const_iterator cit;
	//...
	v.emplace(cit, 1,0);//Noncompliant
}

In this example, creating a ratio with a zero denominator causes an exception. Because the emplace() method might leave the vector v in an invalid state, Polyspace flags the call to the emplace() method.

Check Information

Group: Exception handling
Category: Required, Non-automated

Version History

Introduced in R2022a