noexcept Specifier
Mark non-throwing functions.
What noexcept Declares
The noexcept specifier promises a function will not throw. If it does anyway, the program calls std::terminate.
#include <iostream>
int square(int x) noexcept {
return x * x;
}
int main() {
std::cout << square(5) << "\n";
}Why It Matters
Knowing a function cannot throw lets the compiler and library optimize — for example, choosing moves over copies during container growth.
All lessons in this course
- try, catch, throw
- Exception Safety
- noexcept Specifier
- Custom Exception Types