AUTOSAR C++14 Rule A15-4-5
Checked exceptions that could be thrown from a function shall be specified together with the function declaration and they shall be identical in all function declarations and for all its overriders
Since R2021a
Description
Rule Definition
Checked exceptions that could be thrown from a function shall be specified together with the function declaration and they shall be identical in all function declarations and for all its overriders.
Rationale
In C++, there are no checked exceptions because the compiler does not force functions to
specify or handle the exceptions that the functions might raise. Dynamic exception
specification of the form throw(<>)
is obsolete and error-prone. The
exception specification prescribed in the C++ standard specifies only whether a function
raises an exception or not by using the specifier noexcept
. Because there
is no official way to declare which exceptions might arise from a function, the AUTOSAR
standard requires that each function declaration be accompanied by comments that document
the exception handling of the function. This method of documenting the exceptions is similar
to the JAVA exception handling mechanism.
Use comments to specify a list of exceptions that a static analysis tool must check. Before function declarations, use comments to document which of the checked exceptions are expected in the function.
For a class template, the possible exceptions depend on the template argument. Because you cannot predicts the possible exceptions arising from a class template, this rule does not apply for templates.
Polyspace Implementation
Polyspace® reports a violation of this rule when any of these conditions are true:
A function raises a checked exception but does not document it before its declaration.
A function does not raise all the checked exceptions that are documented in comments before its declaration.
A function documents an unchecked exception.
A function documents an exception but does not define it.
This checker ignores the class member functions that are not called in your code. Because this rule does not apply for templates, Polyspace does not report violations of this rule on templates.
When documenting the checked exception classes, separate different checked exception
classes by using line breaks, except between the class declaration and the documenting
comments. For example, in this code, a line break separates the checked exceptions class
A
and B
. The documenting comment and declaration of
each class are kept
together:
/// @checkedException class A{}; /// @checkedException class B{};
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Exception handling |
Category: Required, Automated |