noexcept Specifier
Mark non-throwing functions.
noexcept Specifier is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Move Operations Should Be noexcept
std::vector only uses your move constructor when reallocating if it is noexcept; otherwise it copies for safety. So mark moves noexcept.
struct Buffer {
int* p;
Buffer(Buffer&& o) noexcept : p(o.p) { o.p = nullptr; }
};Destructors Are noexcept by Default
Since C++11 destructors are implicitly noexcept. Letting an exception escape one terminates the program.
Conditional noexcept
noexcept(expr) makes the promise depend on a compile-time boolean — useful in templates that forward another type's guarantee.
template <class T>
void swapValues(T& a, T& b) noexcept(noexcept(T(std::move(a)))) {
T tmp = std::move(a);
a = std::move(b);
b = std::move(tmp);
}Querying with the noexcept Operator
noexcept(expr) as an operator yields true if the expression is declared not to throw — handy in templates.
#include <iostream>
int f() noexcept { return 1; }
int g() { return 2; }
int main() {
std::cout << noexcept(f()) << " " << noexcept(g()) << "\n"; // 1 0
}noexcept Is Part of the Type
Since C++17, noexcept is part of a function pointer's type, so a throwing function cannot bind to a noexcept pointer.
Do Not Over-Promise
Only mark a function noexcept if you are sure it cannot throw. A false promise that gets violated crashes the whole program.
Good Candidates
Swaps, moves, destructors, simple getters, and arithmetic helpers are natural noexcept functions.
noexcept vs throw()
The old dynamic throw() specification is deprecated and removed. Use noexcept instead in modern C++.
Performance and Safety
noexcept both documents intent and enables optimizations like move-on-realloc — a small annotation with real impact.
Quick Check
Test your understanding of noexcept.
Recap
You learned the noexcept specifier promises no exceptions, enabling optimizations like move-on-reallocation. Mark moves, swaps, and destructors noexcept, use conditional forms in templates, and never over-promise.
Frequently asked questions
Is the “noexcept Specifier” lesson free?
Yes — the full text of “noexcept Specifier” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.
What will I learn in “noexcept Specifier”?
Mark non-throwing functions. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “noexcept Specifier” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- try, catch, throw
- Exception Safety
- noexcept Specifier
- Custom Exception Types