AutosarC++17_03-A15.3.1

Unchecked exceptions should be handled only in main or thread’s main functions

Required inputs: IR

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.

Example
//% $Id: A15-3-1.cpp 271687 2017-03-23 08:57:35Z piotr.tanski $
#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

autosar_exception_model

autosar_exception_model : str = '17_03'

The default model for what counts as an checked / unchecked exception.