AutosarC++17_03-A15.4.1ΒΆ

Dynamic exception-specification shall not be used

Required inputs: IR

Rationale

This feature was deprecated in the 2011 C++ Language Standard (See: DeprecatingException Specifications).

Main issues with dynamic exception specifications are:

  1. Run-time checking: Exception specifications are checked at runtime, so theprogram does not guarantee that all exceptions will be handled. The run-timefailure mode does not lend itself to recovery.
  2. Run-time overhead: Run-time checking requires the compiler to produceadditional code that hampers optimizations.
  3. Unusable in generic code: It is not possible to know what types of exceptionsmay be thrown from templates arguments operations, so a precise exceptionspecification cannot be written.

In place of dynamic exception-specification, use noexcept specification that allows todeclare whether a function throws or does not throw exceptions.

Note: std::unexpected_handler shall not be used.

Example
//% $Id: A15-4-1.cpp 270728 2017-03-16 10:38:20Z piotr.tanski $
#include <stdexcept>
void f1() noexcept;        // Compliant - note that noexcept is the same as
                   // noexcept(true)
void f2() throw();         // Non-compliant - dynamic exception-specification is
                   // deprecated
void f3() noexcept(false); // Compliant
void f4() throw(std::runtime_error); // Non-compliant - dynamic
// exception-specification is deprecated
void f5() throw(
...); // Non-compliant - dynamic exception-specification is deprecated
template <class T> void f6() noexcept(noexcept(T())); // Compliant
See Also
  • C++ Core Guidelines [10]: E.12: Use noexcept when exiting a function because of a throw is impossible or unacceptable
  • open-std.org [17]: open std Deprecating Exception Specifications
  • mill22: A Pragmatic Look at Exception Specifications

Possible Messages

Key

Text

Severity

Disabled

throw_in_decl

Dynamic exception-specification shall not be used.

None

False

Options