AutosarC++17_03-A15.3.4

Catch-all (ellipsis and std::exception) handlers shall be used only in (a) main, (b) task main functions, (c) in functions that are supposed to isolate independent components and (d) when calling third-party code that uses exceptions not according to AUTOSAR C++14 guidelines

Required inputs: IR

Rationale

Catching an exception through catch-all handlers does not provide any detailed information about caught exception. This does not allow to take meaningful actions to recover from an exception other than to re-throw it. This is inefficient and results in code that is difficult to maintain.

Example
//% $Id: A15-3-4.cpp 271752 2017-03-23 12:07:07Z piotr.tanski $
#include <stdexcept>
#include <thread>
extern std::int32_t fn(); // Prototype of external third-party library function
void f1() noexcept(false) {
try {
std::int32_t ret = fn();
// ...
}

// ...
catch (...) // Compliant
{
// Handle all unexpected exceptions from fn() function
}
}
void f2() noexcept(false) {
std::int32_t ret =
fn(); // Non-compliant - can not be sure whether fn() throws or not

if (ret < 10) {
throw std::underflow_error("Error");
}

else if (ret < 20) {
// ...
} else if (ret < 30) {
throw std::overflow_error("Error");
}

else {
throw std::range_error("Error");
}
}
void f3() noexcept(false) {
try {
f2();
}

catch (std::exception &e) // Non-compliant - caught exception is too
                    // general, no information which error occured
{
// Nothing to do
throw;
}
}
void f4() noexcept(false) {
try {
f3();
}

catch (...) // Non-compliant - no information about the exception
{
// Nothing to do
throw;
}
}
class ExecutionManager {
public:
ExecutionManager() = default;
void execute() noexcept(false) {
try {
f3();
}

// ...
catch (std::exception &e) // Compliant
{
// Handle all expected exceptions
} catch (...) // Compliant
{
// Handle all unexpected exceptions
}
}
};
void threadMain() noexcept {
try {
f3();
}

// ...
catch (std::exception &e) // Compliant
{
// Handle all expected exceptions
} catch (...) // Compliant
{
// Handle all unexpected exceptions
}
}
int main(int, char **) {
try {
ExecutionManager execManager;
execManager.execute();
// ...
std::thread t(&threadMain);
// ...
f4();
}

// ...
catch (std::exception &e) // Compliant
{
// Handle all expected exceptions
} catch (...) // Compliant
{
// Handle all unexpected exceptions
}

return 0;
}
See Also
  • none

Possible Messages

Key

Text

Severity

Disabled

catch_all

Use of catch(…).

None

False

catch_std_exception

Catching std::exceptions is too general.

None

False

Options

allow_in_extern_c_functions

allow_in_extern_c_functions : bool = False

Whether to allow a catch-all in extern C functions.
 

allow_in_functions_matching

allow_in_functions_matching : typing.Pattern[str] | None = None

If not None, a re.compile()ed object where functions whose qualified name matches the regex are allowed to contain catch-alls.
 

allow_in_main

allow_in_main : bool = True

Whether to allow a catch-all in the main() function.
 

allow_in_thread_main

allow_in_thread_main : bool = True

Whether to allow a catch-all in thread-main functions.
 

allow_rethrow

allow_rethrow : bool = False

Whether to allow a catch-all with a re-throw.
 

autosar_exception_model

autosar_exception_model

Type: bauhaus.ir.autosar.exceptions.autosar_exceptions.AutosarExceptionModel

Default: <bauhaus.ir.autosar.exceptions.autosar_exceptions.AutosarExceptionModel object at 0x7f3bf46fd820>

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

disallow_std_exception

disallow_std_exception : bool = True

Whether to consider catching the literal std::exception as a catch-all.