C++ noexcept Keyword

The noexcept keyword in C++ is used to indicate that a function does not throw exceptions. It serves as a guarantee to the compiler and the programmer that the function will not propagate exceptions.

The noexcept specifier can be applied to functions, lambdas, and operator overloads. It is particularly useful in performance-critical code, where knowing that a function cannot throw exceptions enables the compiler to optimize better.


Syntax

</>
Copy
return_type function_name(parameters) noexcept {
    // Function body
}
return_type
The type of value returned by the function.
function_name
The name of the function to which the noexcept specifier is applied.
parameters
The input parameters for the function.
noexcept
A specifier that guarantees the function does not throw exceptions.

Examples

Example 1: Basic Usage of noexcept

In this example, you will learn how to declare a function with the noexcept specifier.

</>
Copy
#include <iostream>
using namespace std;

void safeFunction() noexcept {
    cout << "This function does not throw exceptions." << endl;
}

int main() {
    safeFunction();
    return 0;
}

Output:

This function does not throw exceptions.

Explanation:

  1. The function safeFunction is declared with the noexcept specifier, guaranteeing that it does not throw exceptions.
  2. When the function is called in main, it executes normally without throwing any exceptions.

Example 2: Conditional noexcept

In this example, you will learn how to use noexcept conditionally based on a compile-time expression.

</>
Copy
#include <iostream>
using namespace std;

void mayThrow(bool throwException) {
    if (throwException) {
        throw runtime_error("An exception was thrown!");
    }
}

void safeFunction(bool condition) noexcept(noexcept(mayThrow(condition))) {
    if (condition) {
        mayThrow(false); // This will not throw
    }
}

int main() {
    safeFunction(true);
    cout << "Execution continued normally." << endl;
    return 0;
}

Output:

Execution continued normally.

Explanation:

  1. The safeFunction uses conditional noexcept based on whether mayThrow can throw exceptions.
  2. The noexcept operator checks whether the function argument is noexcept at compile time.
  3. Since mayThrow does not throw exceptions in this case, the program executes normally.

Example 3: Lambdas with noexcept

In this example, you will learn how to declare a noexcept lambda.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    auto safeLambda = []() noexcept {
        cout << "This is a noexcept lambda function." << endl;
    };

    safeLambda();
    return 0;
}

Output:

This is a noexcept lambda function.

Explanation:

  1. The lambda function is declared with the noexcept specifier, ensuring it does not throw exceptions.
  2. When the lambda is invoked, it executes normally without throwing any exceptions.

Key Points about noexcept Keyword

  1. The noexcept specifier guarantees that a function does not throw exceptions.
  2. Using noexcept can enable better compiler optimizations and improve performance.
  3. You can use conditional noexcept to determine the specifier based on compile-time conditions.
  4. The noexcept operator evaluates to true or false depending on whether a given function is noexcept.