Skip to content
Learn Netverks

Lesson

Step 28/36 78% through track

exceptions-cpp

Exceptions

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Exceptions: the syntax, patterns, and safety habits you need before advancing in C++.

Exception safety guarantees matter in library APIs—noexcept and RAII interact in reviews.

You will apply Exceptions in contexts like: Game engines, trading systems, desktop apps, and performance-critical libraries.

Write C++ in main.cpp with int main(), click Run on server—the dev runner compiles with c++/g++ -std=c++17 -Wall and runs the binary; read template errors in stderr (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

C++ supports exceptions for error propagation—unlike error codes alone in C, though many teams still use std::optional or expected-style returns for control flow.

try / catch / throw

try {
    throw std::runtime_error("failed");
} catch (const std::exception& ex) {
    std::cerr << ex.what() << "\n";
}

RAII ensures destructors run during stack unwinding—locks and files still release.

Important interview questions and answers

  1. Q: Exceptions vs error codes?
    A: Exceptions separate error path from happy path; codes are predictable in real-time/embedded contexts where exceptions may be banned.
  2. Q: noexcept?
    A: Promises a function won't throw—enables optimizations and documents contracts.

Self-check

  1. What runs during stack unwinding?
  2. Base type to catch standard exceptions?

Tip: RAII means destructors still run during exception unwinding—unlike error-code-only paths in C where cleanup must be manual.

Interview prep

RAII and exceptions?

Stack unwinding runs destructors—resources acquired via RAII still release on throw.

Exceptions vs error codes?

Exceptions separate error paths; error codes suit real-time/embedded contexts where exceptions may be banned.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • throw by value?
  • noexcept when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump