Main Content

AUTOSAR C++14 Rule A15-5-3

The std::terminate() function shall not be called implicitly

Description

Rule Definition

The std::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 remains unhandled. For instance:

    • While handling an exception, it escapes through another function that raises an unhandled exception. For instance, a catch statement or exception handler invokes another function that raises an unhandled exception.

    • An empty throw statement raises an unhandled exception again.

    For more details, see Uncaught exception

  • A class destructor raises an exception.

  • 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(){
	try{
		//...
		obj localObject = globalObject; //Noncompliant
		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. For instance, the main function has no catch blocks to handle the exception raised by the copy constructor of obj. This unhandled exception might result in an implicit call to std::terminate(). Polyspace reports a violation on the statement where the copy constructor is invoked.

  • 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, Automated

Version History

Introduced in R2019a

expand all