AutosarC++17_10-A15.3.1¶
Unchecked exceptions should be handled only in main or thread’s main functions
Required inputs: IR
| Level | advisory |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
Unchecked exceptions (e.g. bad_alloc, out_of_range, length_error, invalid_argument) either are a consequence of faulty logic within the program or are unpreventable and the program can not recover from them with meaningful action. In this case, propagate the exception up the call tree to the main (or thread’s main) function where one common handler will be executed.
Exceptions
This rule does not apply to C++ Standard Library, as it can provide different error handling mechanisms that depend on user input or usage.
Functions listed in the rule A15-5-1 have to handle unchecked exceptions internally to meet requirements of the rule A15-5-2.
Example
//% $Id: A15-3-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
#include <stdexcept>
class OutOfMemory : public std::logic_error // Unchecked exception
{
public:
using std::logic_error::logic_error;
};
void F1() noexcept(false) {
// ...
throw OutOfMemory("Not enough memory");
}
void F2() noexcept {
try {
F1();
} catch (OutOfMemory &e) // Non-compliant - program is not able to handle an
// OutOfMemory error in a meaningful way, the error
// will still exist
{
// Handle an exception
}
}
void F3() noexcept(false) {
// ...
try {
// ...
F1();
} catch (OutOfMemory &e) {
// Nothing to do, just re-throw
throw; // Non-compliant - it is inefficient to catch and re-throw an
// error that can not be handled in f3()
}
}
void F4() noexcept(false) {
// ...
F1(); // Compliant - OutOfMemory error can not be handled in f4()
// ...
}
int main(int, char **) {
try {
F4();
} catch (OutOfMemory &e) // Compliant - OutOfMemory caught in main() function
// so the program can clean-up and exit correctly
{
// Report the error and exit from the program correctly
}
return 0;
}
See Also
- none
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
unchecked_exception |
Handle unchecked exceptions only in main or thread’s main function. |
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
autosar_exception_model¶
autosar_exception_model : str = '17_03'