AUTOSAR C++14 Rule A15-2-2
If a constructor is not noexcept and the constructor cannot finish object initialization, then it shall deallocate the object's resources and it shall throw an exception
Since R2021a
Description
Rule Definition
If a constructor is not noexcept and the constructor cannot finish object initialization, then it shall deallocate the object's resources and it shall throw an exception.
Rationale
When a constructor abruptly terminates due to unhandled exception or failed dynamic
resource allocation, it might leave some objects in a partially constructed object, which is
undefined behavior. Before raising exceptions in class constructors, deallocate the already
allocated resources. When allocating resources, specify the new
operation
as std::nothrow
. Alternatively, perform the resource allocation in a
try
or function-try
block to handle exceptions that
might arise from a failed allocation.
Polyspace Implementation
Polyspace® flags a throw
or new
statement outside a
try
block in a non-noexcept
class constructor if the
statement might result in resource leak. For instance:
A
throw
statement outside atry
block is flagged if the allocated resources are not deallocated before the statement.A
new
statement is flagged if there are more than onenew
statement in succession and the latter ones is not specified asstd::nothrow
or wrapped in atry
orfunction-try
block.
Polyspace ignores classes that remain unused in your code.
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, Partially automated |
Version History
Introduced in R2021a