AutosarC++17_03-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
Required inputs: IR, StaticSemanticAnalysis
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
In C++ language, all exceptions are unchecked, because the compiler does not force to either handle the exception or specify it. Because dynamic-exception specification is obsolete and error prone, an alternative mechanism of specifying checked exceptions using C++ comments along with function declarations is used. It is a concept that is based on Java exception handling mechanism.
When analyzing a given function f, a static code analysis needs to analyze functions invoked by f and analyze if they throw any checked exceptions that are not caught by f and not listed by f in the function comment.
Exceptions
Within generic code, it is not generally possible to know what types of exceptions may be thrown from operations on template arguments, so a precise exception specification cannot be written. Therefore, this rule does not apply for templates.
Example
//% $Id: A15-4-5.cpp 271752 2017-03-23 12:07:07Z piotr.tanski $
#include <cstdint>
#include <stdexcept>
class CommunicationError : public std::exception {
// Implementation
};
class BusError : public CommunicationError {
// Implementation
};
class Timeout : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
// Implementation
};
/// @throw CommunicationError Communication error
/// @throw BusError Bus error
/// @throw Timeout On send timeout exception
void send1(
std::uint8_t *buffer,
std::uint8_t bufferLength) noexcept(false) // Compliant - All and only
// those checked exceptions
// that can be thrown are
// specified
{
// ...
throw CommunicationError();
// ...
throw BusError();
// ...
throw Timeout("Timeout reached");
// ...
}
/// @throw CommunicationError Communication error
void send2(
std::uint8_t *buffer,
std::uint8_t bufferLength) noexcept(false) // Non-compliant - checked
// exceptions that can be
// thrown are missing from
// specification
{
// ...
throw CommunicationError();
// ...
throw Timeout("Timeout reached");
// ...
}
class MemoryPartitioningError : std::exception {
// Implementation
};
/// @throw CommunicationError Communication error
/// @throw BusError Bus error
/// @throw Timeout On send timeout exception
/// @throw MemoryPartitioningError Memory partitioning error prevents message
/// from being sent.
void send3(
std::uint8_t *buffer,
std::uint8_t bufferLength) noexcept(false) // Non-compliant - additional
// checked exceptions are
// specified
{
// ...
throw CommunicationError();
// ...
throw Timeout("Timeout reached");
// ...
}
See Also
- Effective Java 2nd Edition [14]: Item 62: Document all exceptions thrown by each method
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
differing_documented |
Documented exceptions differ from overridden method. |
None |
False |
document_exception |
Document checked exception {} using {}. |
None |
False |
superfluous_documented |
Documented exceptions {} probably never thrown. |
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'
exclude_exception_base_classes¶
exclude_exception_base_classes : set[bauhaus.analysis.config.QualifiedName] = set()
ignore_definitions_if_declaration_documented¶
ignore_definitions_if_declaration_documented : bool = False
include_template_instances¶
include_template_instances : bool = False
match_qualified_name¶
match_qualified_name : bool = True
throw_marker¶
throw_marker : str = '@throw'