โ† All CPP Flashcard Decks

Exception Handling & Error Management Flashcards

7 cards from real CPP practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 7 Exception Handling & Error Management flashcards as text
  1. What is the RAII idiom's primary role in exception-safe code?

    Answer: Ensuring resources are released automatically when objects go out of scope

    RAII ties resource lifetime to object scope so that destructors automatically release resources even when exceptions are thrown.

  2. What exception does std::vector::at() throw when the index is out of bounds?

    Answer: std::out_of_range

    std::vector::at() throws std::out_of_range (inheriting from std::logic_error) when the index is invalid, unlike operator[] which is undefined behavior.

  3. What does the strong exception guarantee mean for a function?

    Answer: If an exception is thrown, the program state is rolled back to what it was before the call

    The strong guarantee means either the operation succeeds or, if an exception occurs, the program state is unchanged (commit-or-rollback semantics).

  4. Which of the following correctly uses std::current_exception() and std::rethrow_exception()?

    Answer: auto ep = std::current_exception(); std::rethrow_exception(ep);

    std::current_exception() captures the active exception as an exception_ptr, which can later be rethrown with std::rethrow_exception().

  5. What is the effect of declaring a move constructor as noexcept on container operations like std::vector resize?

    Answer: The vector will use the move constructor instead of copy constructor during reallocation

    std::vector uses move instead of copy during reallocation only if the move constructor is noexcept, to preserve the strong exception guarantee.

  6. What is the difference between std::runtime_error and std::logic_error?

    Answer: logic_error signals conditions detectable before execution; runtime_error signals conditions detectable only during execution

    std::logic_error represents bugs detectable before runtime (precondition violations), while std::runtime_error represents unpredictable runtime conditions (I/O failures, overflow).

  7. What happens to local objects in a function when an exception propagates through it?

    Answer: Their destructors are called in reverse order of construction during stack unwinding

    Stack unwinding during exception propagation calls destructors of all automatic-storage-duration objects in reverse construction order.