Exceptions in C++
C++ exceptions separate exceptional failure handling from normal execution using try, throw, and catch.
Summary
C++ exceptions separate exceptional failure handling from normal execution using try, throw, and catch. A thrown exception propagates through function calls until a compatible handler is found. Handlers are checked in order, so derived exception handlers must precede base exception handlers, with catch (...) last.
Catching class exceptions by const reference avoids copying and preserves access to their dynamic type. std::exception is a standard base class for many library exceptions; its what() member supplies a diagnostic description. A bare throw; inside a handler rethrows the current exception without replacing its type.
During stack unwinding, fully constructed automatic objects in exited scopes are destroyed. RAII uses this destruction to release owned resources such as memory, files, and locks. Exception-safety guarantees describe an operation's behavior when failure occurs:
| Guarantee | Failure behavior |
|---|---|
| Basic | Invariants remain valid and resources do not leak. |
| Strong | Program state remains unchanged. |
| Nothrow | The operation emits no exceptions. |
A noexcept function must not allow an exception to escape; violating this invokes std::terminate. An exception escaping a destructor during stack unwinding also causes termination.
Common Misconceptions: Handler order matters; a base handler can intercept derived exceptions. Stack unwinding destroys fully constructed automatic objects, not only objects directly involved in the failure. noexcept does not promise recovery if an exception escapes.
🧠 Key Concepts
- Exception propagation
- Handler ordering
- Const reference catching
- Stack unwinding
- RAII
- Exception rethrowing
- Exception safety guarantees
- noexcept
🧠 Quick Check
See what you remember from the summary.
A try block has handlers for a derived exception class, its base class, and any remaining exception type. How should they be ordered?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Exceptions in C++
📘 Overview C++ exceptions transfer control from a throw expression to a matching exception handler. The try, throw, and catch constructs separate exceptional failure handling from normal program execution.
🧠 Key Idea A thrown exception propagates through function calls until a compatible catch handler is found. During stack unwinding to that handler, destructors release resources owned by local objects.
⚔️ Core Details: - A try block encloses operations whose exceptions may be handled by its associated catch handlers. - A throw expression creates an exception object and transfers control toward a matching handler. - Handlers for derived exception types must precede handlers for their base types because C++ selects the first matching handler. - Catching class exceptions by const reference avoids copying and preserves access to the original exception's dynamic type. - Stack unwinding destroys fully constructed automatic objects in the scopes exited during exception propagation. - A bare throw; inside a handler rethrows the currently handled exception without replacing its type.
🎯 Why It Matters: - Exceptions allow failures to cross multiple function calls without requiring every intermediate function to return and inspect error codes. - RAII makes resource cleanup reliable during exception propagation, including memory ownership, file handles, and locks. - Exception-safety guarantees clarify whether a failed operation preserves valid state or leaves program state unchanged. - Throwing destructors are dangerous because an exception escaping a destructor during stack unwinding causes program termination.
🧠 Quick Recall: - std::exception - Standard base class for many library exceptions; what() provides a diagnostic description. - catch (const std::exception& e) - Handles std::exception and publicly derived exception objects by const reference. - catch (...) - Catch-all handler for any C++ exception type; placed after more specific handlers. - noexcept - Specifies that a function must not allow an exception to escape; violating this invokes std::terminate. - Exception-safety guarantees - Basic: invariants remain valid and resources do not leak. Strong: failure leaves state unchanged. Nothrow: the operation does not emit exceptions.
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Engineering notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.