noexcept Operator in C++ 11 (original) (raw)

Last Updated : 10 Jun, 2026

The noexcept operator was introduced in C++11 to perform a compile-time check of an expression's exception specification. It determines whether an expression is considered non-throwing without actually evaluating it.

#include using namespace std;

// Function that may throw an exception int divide(int a, int b) { if (b == 0) { throw runtime_error("Error: Division by zero"); } return a / b; }

// Function that will not throw any exceptions (using // noexcept) double safeDivide(double a, double b) noexcept { if (b == 0) { // In this case, std::terminate will be called if b // is zero cerr << "Error: Division by zero in safeDivide" << std::endl; terminate(); } return a / b; }

int main() { cout << "noexcept value for divide(): " << noexcept(divide(10, 0)) << "\n";

cout << "noexcept value for safeDivide(): "
     << noexcept(safeDivide(10.0, 0.0));

return 0;

}

`

Output

noexcept value for divide(): 0 noexcept value for safeDivide(): 1

**Explanation

**Note: If an exception escapes from a function declared noexcept, the program calls std::terminate().

Syntax

noexcept(expression)

Where, expression is the expression whose exception specification is checked.

**Return Value

Using noexcept with Function Templates

The noexcept operator is often used in templates to conditionally determine whether a function should be marked as noexcept based on the operations it performs.

C++ `

#include <bits/stdc++.h> using namespace std;

template void process(const T& value) noexcept(noexcept(declval().size())) { // Try to call the size() member function of T // If T's size() can throw, this function won't be // noexcept for T // If T's size() is noexcept, this // function will be noexcept for T cout << "Size: " << value.size() << endl; }

// main function int main() { vector numbers = { 1, 2, 3, 4, 5 };

// Won't throw exceptions for std::vector
process(numbers);
// May throw exceptions for int
process(42);

return 0;

}

`

**Output

./Solution.cpp: In instantiation of 'void process(const T&) [with T = int]':
./Solution.cpp:23:15: required from here
./Solution.cpp:5:6: error: request for member 'size' in 'std::declval()', which is of non-class type 'int'
void process(const T& value)
^
./Solution.cpp: In instantiation of 'void process(const T&) [with T = int]':
./Solution.cpp:23:15: required from here
./Solution.cpp:5:6: error: request for member 'size' in 'std::declval()', which is of non-class type 'int...

**Explanation

When to Use noexcept

Use noexcept when:

When Not to Use noexcept

Avoid using noexcept when: