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
What is the purpose of std::nothrow when used with the new operator?
Answer: It makes new return nullptr instead of throwing std::bad_alloc on failure
new(std::nothrow) returns nullptr rather than throwing std::bad_alloc when memory allocation fails.
Which standard exception type should be thrown when a function argument is outside the valid range?
Answer: std::invalid_argument
std::invalid_argument is thrown when a function receives an argument with an invalid value regardless of domain constraints.
What does std::terminate() do by default when called?
Answer: Calls std::abort()
By default, std::terminate() calls std::abort(), which immediately ends the program without stack unwinding.
What happens if no catch handler matches a thrown exception?
Answer: std::terminate() is called directly
If no matching handler is found after searching all active try blocks, std::terminate() is called directly in C++11 and later.
Which of these exception types inherits from std::logic_error?
Answer: std::length_error
std::length_error derives from std::logic_error, as do std::invalid_argument, std::domain_error, and std::out_of_range.
What is a function-try-block used for?
Answer: Wrapping the entire function body including member initializer lists
A function-try-block wraps the entire function including its initializer list, which is the only way to catch exceptions from base/member constructors.
Which of the following is NOT a valid use of catch(...)?
Answer: Accessing the caught exception object's members
catch(...) catches any exception but provides no named object, so you cannot access the exception's members without rethrowing and catching by type.