AutosarC++18_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 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#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