0Pricing
C++ Academy · Lesson

Exception Safety

Write exception-safe code.

Exception Safety is a free C++ Academy lesson on CoddyKit — lesson 2 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 Exception Safety Means

Exception safety describes what guarantees your code keeps when an exception is thrown partway through an operation.

The No-Throw Guarantee

The strongest level: the operation never throws. Destructors, swaps, and move operations should aim for this and be marked noexcept.

The Strong Guarantee

If the operation throws, the program state is rolled back as if it never started — a commit or rollback semantic.

The Basic Guarantee

If the operation throws, no resources leak and all invariants hold, though the exact state may have changed.

The No Guarantee

The weakest: an exception may leave the program in a broken or leaking state. Avoid writing such code.

RAII Provides the Basic Guarantee

By wrapping resources in RAII objects, stack unwinding frees them automatically — giving you the basic guarantee almost for free.

#include <memory>
#include <stdexcept>

void process() {
    auto buf = std::make_unique<int[]>(100);  // freed even if throw
    throw std::runtime_error("oops");          // no leak
}

Copy-and-Swap for Strong Safety

A common technique: do all the risky work on a copy, then swap it in. The swap is no-throw, so the change is all-or-nothing.

void assign(Widget& target, const Widget& src) {
    Widget tmp = src;          // may throw, target untouched
    using std::swap;
    swap(target, tmp);         // no-throw commit
}

Order Operations to Commit Last

Do every operation that can throw before any irreversible change. Once you start mutating shared state, you should not be able to fail.

Beware Partial Updates

Updating two members where the second can throw leaves the object inconsistent. Prepare both, then commit both with non-throwing steps.

Destructors Must Not Throw

A throwing destructor during unwinding calls std::terminate. Keep destructors no-throw to preserve safety guarantees.

Choosing a Guarantee

Aim for the strong guarantee where feasible, always provide at least the basic one, and mark genuinely no-throw operations noexcept.

Quick Check

Test your understanding of exception safety.

Recap

You learned the four levels of exception safety: no-throw, strong, basic, and none. RAII gives the basic guarantee automatically; copy-and-swap and committing last give the strong guarantee. Keep destructors and swaps no-throw.

Frequently asked questions

Is the “Exception Safety” lesson free?

Yes — the full text of “Exception Safety” 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 “Exception Safety”?

Write exception-safe code. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Exception Safety” 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

  1. try, catch, throw
  2. Exception Safety
  3. noexcept Specifier
  4. Custom Exception Types
← Back to C++ Academy