AutosarC++18_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
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | non-automated |
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 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#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¶
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_in_extern_c_functions¶
allow_in_extern_c_functions : bool = False
extern C functions.
allow_in_functions_matching¶
allow_in_functions_matching : typing.Pattern[str] | None = None
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
allow_in_thread_main¶
allow_in_thread_main : bool = True
allow_rethrow¶
allow_rethrow : bool = False
autosar_exception_model¶
autosar_exception_model
The default model for what counts as an checked / unchecked exception.Type: bauhaus.ir.autosar.exceptions.autosar_exceptions.AutosarExceptionModel
Default:
<bauhaus.ir.autosar.exceptions.autosar_exceptions.AutosarExceptionModel object at 0x7f3bf46fd820>
disallow_std_exception¶
disallow_std_exception : bool = True