Main Content

MISRA C++:2008 Rule 15-5-3

The terminate() function shall not be called implicitly

Description

Rule Definition

The terminate() function shall not be called implicitly.

Polyspace Implementation

The checker flags situations that might result in calling the function std::terminate() implicitly. These situations might include:

  • An exception escapes uncaught. This also violates MISRA C++:2008 Rule 15-3-2. For instance:

    • Before an exception is caught, it escapes through another function that throws an uncaught exception. For instance, a catch statement or exception handler invokes a copy constructor that throws an uncaught exception.

    • An empty throw expression raises an uncaught exception again.

  • A class destructor raises an exception. Exceptions in destructors also violates MISRA C++:2008 Rule 15-5-1.

  • A termination handler that is passed to std::atexit raises an unhandled exception.

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 <stdexcept>
#include <new>
class obj
{
public:
	obj() noexcept(false){}
	obj(const obj& a){
		//...
		throw -1;
	}
	~obj()
	{
		try{
			// ...
			throw std::runtime_error("Error2"); // Noncompliant
		}catch(std::bad_alloc& e){
			
		}
	}
};
obj globalObject;
void atexit_handler(){//Noncompliant
	throw std::runtime_error("Error in atexit function");
}
void main(){//Noncompliant
	try{
		//...
		obj localObject = globalObject;
		std::atexit(atexit_handler);
	}catch(std::exception& e){
		
	}
}

In this example, Polyspace flags unhandled exceptions because they result in implicit calls to std::terminate().

  • The destructor ~obj() does not catch the exception raised by the throw statement. The unhandled exception in the destructor results in abrupt termination of the program through an implicit call to std::terminate. Polyspace flags the throw statement in the destructor of obj.

  • The main() function does not handle all exceptions raised in the code. Because an unhandled exception might result in an implicit call to std::terminate(), Polyspace flags the main() function.

  • The termination handler atexit_handler raises an uncaught exception. The function atexit_handler executes after the main finishes execution. Unhandled exceptions in this function cannot be handled elsewhere, leading to an implicit call to std::terminate(). Polyspace flags the function.

Check Information

Group: Exception Handling
Category: Required

Version History

Introduced in R2018a

expand all