AutosarC++17_10-A15.3.3

There shall be at least one exception handler to catch all otherwise unhandled exceptions

Required inputs: IR

Rationale

If a program throws an unhandled exception in main function, as well as in init thread function, the program terminates in an implementation-defined manner. In particular, it is implementation-defined whether the call stack is unwound, before termination, so the destructors of any automatic objects may or may not be executed. By enforcing the provision of a “last-ditch catch-all”, the developer can ensure that the program terminates in a consistent manner.

Note that the objective of the previous rule is that a program should catch all exceptions that it is expected to throw. This rule’s objective is to ensure that exceptions that were not expected are also caught.

Example
//% $Id: A15-3-3.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <stdexcept>
int MainGood(int, char **) // 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 MainBad(int,
            char **) // Non-compliant - unexpected exceptions are not caught
{
  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;
}
void ThreadMainGood() // Compliant
{
  try {
    // thread code
  } catch (std::exception &e) {
    // Handle all expected exception
  } catch (...) {
    // Handle all unexpected exception
  }
}
void ThreadMainBad() // Non-compliant - unexpected exceptions are not caught
{
  try {
    // thread code
  } catch (std::exception &e) {
    // Handle all expected exceptions
  }

  // Uncaught unexpected exception will cause an immediate program termination
}
See Also
  • MISRA C++ 2008 [6]: 15-3-2 There should be at least one exception handler to catch all otherwise unhandled exceptions.
  • SEI CERT C++ [9]: ERR51-CPP. Handle all exceptions
  • Effective Java 2nd Edition [14]: Item 65: Don’t ignore exceptions

Possible Messages

Key

Text

Severity

Disabled

catch_all_may_throw

Catch-all body may throw exception

None

False

may_throw_outside_catch_all

Code outside catch-all block may throw

None

False

missing_catch_all

Catch-all required around main program body

None

False

missing_catch_handler

Handler for {} needed

None

False

Options

allow_partial_catch_all

allow_partial_catch_all : bool = False

Allow code outside the catch-all block that cannot propagate an exception.
 

external_base_exceptions

external_base_exceptions : set[str] = set()

Sequence of external/third-party exception base-classes that should be caught.
 

inspect_thread_main

inspect_thread_main : bool = True

Whether to also inspect thread main functions.
 

search_catch_all_for_nested_throwing

search_catch_all_for_nested_throwing : bool = False

The catch-all block should also be scanned for throwing.
 

std_base_exceptions

std_base_exceptions : set[str] = set()

Sequence of cpp-std exception base-classes that should be caught.