AutosarC++18_10-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
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | partially-automated |
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.
Exceptions hierarchy from external libraries may be completely separate from C++ Standard Library std::exception. Handling such base exception classes separately may provide some additional information about application termination causes.
Example
//% $Id: A15-3-3.cpp 309502 2018-02-28 09:17:39Z michal.szczepankiewicz $
#include <stdexcept>
//base exception class from external library that is used
class ExtLibraryBaseException {};
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 (ExtLibraryBaseException &e)
{
// Handle all expected exceptions
// from an external library
}
catch (std::exception& e)
{
// Handle all expected exceptions
}
catch (...)
{
// Handle all unexpected exceptions
}
return 0;
}
int MainBad(int, char**) // Non-compliant - neither unexpected exceptions
// nor external libraries exceptions are 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 (ExtLibraryBaseException &e)
{
// Handle all expected exceptions
// from an external library
}
catch (std::exception& e)
{
// Handle all expected exception
}
catch (...)
{
// Handle all unexpected exception
}
}
void ThreadMainBad() // Non-compliant - neither unexpected exceptions
// nor external libraries exceptions are 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 [7]: 15-3-2 There should be at least one exception handler to catch all otherwise unhandled exceptions.
- SEI CERT C++ [10]: ERR51-CPP. Handle all exceptions
- Effective Java 2nd Edition [15]: 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¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
allow_partial_catch_all¶
allow_partial_catch_all : bool = False
external_base_exceptions¶
external_base_exceptions : set[str] = set()
inspect_thread_main¶
inspect_thread_main : bool = True
search_catch_all_for_nested_throwing¶
search_catch_all_for_nested_throwing : bool = False
std_base_exceptions¶
std_base_exceptions : set[str] = {'std::exception'}