Main Content

AUTOSAR C++14 Rule A15-3-3

Main function and a task main function shall catch at least: base class exceptions from all third-party libraries used, std::exception and all otherwise unhandled exceptions

Since R2020b

Description

Rule Definition

Main function and a task main function shall catch at least: base class exceptions from all third-party libraries used, std::exception and all otherwise unhandled exceptions.

Rationale

During the execution of main() or a task main function, different exceptions can arise. For instance:

  • Explicitly raised exceptions of class std::exception

  • Exceptions arising from the third-party libraries that you use

  • Unexpected exceptions

If any of these exceptions cannot be matched to a handler, the compiler implicitly invokes the function std::terminate() to abnormally terminate program execution. Depending on the hardware and software that you use, this termination process might invoke std::abort() to abort program execution without deleting the variables in the stack. Such an abnormal termination results in memory leaks and security vulnerabilities.

Unhandled exceptions might cause an abnormal termination of the program execution, leading to memory leaks and security vulnerabilities. To avoid these issues, execute the operations of main() or task main functions in a try-catch block. In the catch blocks:

  • Handle exceptions of type std::exception explicitly in appropriate catch blocks.

  • Handle the base class of exceptions arising from third-party libraries.

  • Handle unexpected exceptions in a catch(...) block.

Polyspace Implementation

  • Polyspace® flags a main() function or a task main function if :

    • Unhandled exceptions are raised in the function. For example, exceptions that are raised outside the try-catch block or in a catch block might remain unhandled.

    • The function does not have a try-catch block.

    • The function does not have catch blocks to explicitly handle std::exception type exceptions.

    • The function does not have a catch-all or catch(...) blocks to handle unexpected exceptions.

  • Polyspace does not check if exceptions from third-party libraries are handled.

  • Polyspace flags a main() function or a task main function even if the unhandled exception might not be raised.

Polyspace detects the main() function. To specify a function as a task main function, use these compilation options:

  • -entry-points <name>

  • -cyclic-tasks <name>

  • -interrupts <name>

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

This example shows how Polyspace flags main() and task main functions that do not handle all exceptions. To specify the functions Noncompliant, Noncompliant2, and Compliant2 as task main functions, use the compile option -entry-points Noncompliant,Noncompliant2,Compliant.

#include <stdexcept>
void f_throw() {             // Compliant
  throw 1;
}
void Noncompliant()     // Noncompliant 
{
  try {

  } catch (std::exception& e) {
	  f_throw();              // throw
  } catch (...) {
	  throw;
  }
}
int Noncompliant2()     // Noncompliant 
{
  f_throw();              // throw
  try {

  } catch (std::exception& e) {
  } catch (...) {
  }
  return 0;
}
int Compliant()   // Compliant
{

  try {
    // program code
  } catch (std::runtime_error& e) {
    // Handle runtime errors
  } catch (std::logic_error& e) {
    // Handle logic errors
  } catch (std::exception& e) {
    // Handle all expected exceptions
  }catch (...) {
    // Handle all unexpected exceptions
  }

  return 0;
}
int main()   // Noncompliant
{

  try {
    // program code
  } catch (std::runtime_error& e) {
    // Handle runtime errors
  } catch (std::logic_error& e) {
    // Handle logic errors
  } catch (std::exception& e) {
    // Handle all expected exceptions
  }
  return 0;
}

  • The function f_throw() exits with an unhandled exception. Because this function is not a main or task main function, Polyspace does not flag it.

  • The function Noncompliant() and Noncompliant2() are specified as task main functions. In these functions, f_throw raises an exception that is not handled. Because these task main functions do not handle all the exceptions that might arise, Polyspace flags them. Enclose operations that might raise an exception in a try-catch block to handle exceptions that might arise.

  • The function main() does not have a catch(...) block to handle any unexpected exceptions. Because the main() function does not handle all the exceptions that might arise, Polyspace flags it.

  • The function Compliant is specified as a task main function. This function has a catch for all the exceptions that might arise. Polyspace does not flag it.

Check Information

Group: Exception handling
Category: Required, Partially automated

Version History

Introduced in R2020b