Main Content

MISRA C++:2008 Rule 15-4-1

If a function is declared with an exception-specification, then all declarations of the same function (in other translation units) shall be declared with the same set of type-ids

Description

This checker is deactivated in a default Polyspace® as You Code analysis. See Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access).

Rule Definition

If a function is declared with an exception-specification, then all declarations of the same function (in other translation units) shall be declared with the same set of type-ids.

Rationale

Declaring a function with different exception specification in different source files leads to undefined behavior.

Polyspace Implementation

Polyspace reports a violation is there is a mismatch in the exception specification of a function in different source files.

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

In this example, the function foo() is specified to raise std::bad_alloc exceptions in src1.cpp. In another file src2.cpp, foo() is specified to raise std::exception, perhaps inadvertently. The mismatch leads to undefined behavior in the function bar(). Polyspace reports a violation of this rule.

//src1.cpp
#include<new>
void foo() throw(std::bad_alloc){ //Noncompliant
	try{
	int *p1 = new int[50]; 
	}catch(...){
		//....
		std::bad_alloc e;
		throw e;
	}
}

//src2.cpp
#include<new>
#include<stdexcept>
extern bool condition();
extern void foo() throw(std::exception);
void bar() {
	foo();
	
}

Check Information

Group: Exception Handling
Category: Required

Version History

Introduced in R2013b