C++ noexcept Keyword
Example
Create a function that does not throw exceptions:
int add(int x, int y) noexcept {
return x + y;
}
int main() {
cout << add(5, 3);
return 0;
}
Definition and Usage
The noexcept keyword specifies that a function does not throw exceptions (errors that can be caught with try...catch). Write it after the parentheses of the function, like int add(int x, int y) noexcept.
Use it on functions that never throw an exception, like simple calculations. It tells other programmers, and the compiler, that a call to the function will not cause an exception. If a noexcept function throws an exception anyway, and does not catch it, the program stops immediately.
Related Pages
Read more about exceptions in our C++ Exceptions Tutorial.