AutosarC++19_03-A15.4.4

A declaration of non-throwing function shall contain noexcept specification

Required inputs: IR, StaticSemanticAnalysis

Rationale

Noexcept specification is a method for a programmer to inform the compiler whether or not a function should throw exceptions. The compiler can use this information to enable certain optimizations on non-throwing functions as well as enable the noexcept operator, which can check at compile time if a particular expression is declared to throw any exceptions.

Noexcept specification is also a method to inform other programmers that a function does not throw any exceptions.

A non-throwing function needs to declare noexcept specifier. A function that may or may not throw exceptions depending on a template argument, needs to explicitly specify its behavior using noexcept(<condition>) specifier.

Note that it is assumed that a function which does not contain explicit noexcept specification throws exceptions, similarly to functions that declare noexcept(false) specifier.

Example
//% $Id: A15-4-4.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
    #include <iostream>
    #include <stdexcept>
    void F1(); // Compliant - f1, without noexcept specification, declares to throw
                // exceptions implicitly
    void F2() noexcept;         // Compliant - f2 does not throw exceptions
    void F3() noexcept(true);   // Compliant - f3 does not throw exceptions
    void F4() noexcept(false); // Compliant - f4 declares to throw exceptions
    void F5() noexcept          // Compliant - f5 does not throw exceptions
    {
        try
        {
              F1();   // Exception handling needed, f1 has no noexcept specification
        }

        catch (std::exception& e)
        {
            // Handle exceptions
        }

        F2();    // Exception handling not needed, f2 is noexcept
        F3();    // Exception handling not needed, f3 is noexcept(true)

        try
        {
              F4();   // Exception handling needed, f4 is noexcept(false)
        }

        catch (std::exception& e)
        {
            // Handle exceptions
        }
    }
    template <class T>
    void F6() noexcept(noexcept(T()));    // Compliant - function f6() may be
                                          // noexcept(true) or noexcept(false)
                                          // depending on constructor of class T
    template <class T>
    class A
    {
       public:
         A() noexcept(noexcept(T())) // Compliant - constructor of class A may be
         // noexcept(true) or noexcept(false) depending on
         // constructor of class T
         {
         }
    };
    class C1
    {
       public:
         C1()
         noexcept(
              true) // Compliant - constructor of class C1 does not throw exceptions
         {
         }
         // ...
    };
    class C2
    {
       public:
         C2() // Compliant - constructor of class C2 throws exceptions
         {
         }
        // ...
    };
    void F7() noexcept // Compliant - f7 does not throw exceptions
    {
        std::cout << noexcept(A<C1>()) << ’\n’; // prints ’1’ - constructor of
                                                // A<C1> class is noexcept(true)
                                                // because constructor of C1 class
                                                // is declared to be noexcept(true)
        std::cout << noexcept(A<C2>()) << ’\n’; // prints ’0’ - constructor of
                                                // A<C2> class is noexcept(false)
                                                // because constructor of C2 class
                                                // has no noexcept specifier
    }
See Also
  • none

Possible Messages

Key

Text

Severity

Disabled

implicit_noexcept_spec_violation_without

Function implicitly declared noexcept(false) but no exceptions will be thrown.

None

False

noexcept_spec_missing

Explicit noexcept-specification missing.

None

False

noexcept_spec_violation_with

Exception violates function’s noexcept-specification.

None

False

noexcept_spec_violation_without

Function declared noexcept(false) but no exceptions will be thrown.

None

False

Options

exclude_exception_base_classes

exclude_exception_base_classes : set[bauhaus.analysis.config.QualifiedName] = set()

Exclude issues for the exception types mentioned in this set of qualified names. Also excludes classes derived from those class names as well as pointers or references to any of these class types.
 

generate_violation_path

generate_violation_path : bool = True

Whether to compute a trace for the exception. This improves the usability of the violation description, but requires additional computing which might slow down the rule.
 

ignore_constructor_destructor

ignore_constructor_destructor : bool = False

Whether to ignore escaping exceptions from constructors and destructors.
 

ignore_implicit_noexcept_spec_lambda

ignore_implicit_noexcept_spec_lambda : bool = False

Whether to ignore lambda call operator functions with implicit noexcept specifications.
 

ignore_throwing_functions

ignore_throwing_functions : bool = True

Whether to ignore noexcept specification violations on function that actually throw an exception.
 

ignore_unknown_routines

ignore_unknown_routines : bool = True

Whether to ignore extern or only declared routines.
 

report_noexcept_false_violations

report_noexcept_false_violations : bool = True

Whether to report cases where the function is declarared noexcept(false), but no exceptions are ever thrown (directly or indirectly).