CPP Exception Handling & Error Management 3 — Questions and Answers
Question 1: What is the purpose of std::nothrow when used with the new operator?
- It disables exception handling globally
- It makes new return nullptr instead of throwing std::bad_alloc on failure (Correct answer)
- It pre-allocates memory to avoid exceptions
- It throws a custom exception on allocation failure
Correct 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.
Question 2: Which standard exception type should be thrown when a function argument is outside the valid range?
- std::runtime_error
- std::domain_error
- std::out_of_range
- std::invalid_argument (Correct answer)
Correct answer: std::invalid_argument
std::invalid_argument is thrown when a function receives an argument with an invalid value regardless of domain constraints.
Question 3: What does std::terminate() do by default when called?
- Throws a std::terminate_exception
- Calls std::abort() (Correct answer)
- Calls std::exit(1)
- Unwinds the stack and prints a message
Correct answer: Calls std::abort()
By default, std::terminate() calls std::abort(), which immediately ends the program without stack unwinding.
Question 4: What happens if no catch handler matches a thrown exception?
- The exception is silently discarded
- std::unexpected() is called then std::terminate()
- std::terminate() is called directly (Correct answer)
- The program pauses waiting for a debugger
Correct 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.
Question 5: Which of these exception types inherits from std::logic_error?
- std::runtime_error
- std::overflow_error
- std::range_error
- std::length_error (Correct answer)
Correct 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.
Question 6: What is a function-try-block used for?
- Wrapping the entire function body including member initializer lists (Correct answer)
- Replacing all return statements with exception-safe alternatives
- Catching exceptions thrown by inline functions
- Enabling noexcept on non-noexcept functions
Correct 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.
Question 7: Which of the following is NOT a valid use of catch(...)?
- Catching all exception types including non-class exceptions
- Rethrowing the caught exception with throw;
- Accessing the caught exception object's members (Correct answer)
- Serving as a final fallback handler
Correct 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.
What is the purpose of std::nothrow when used with the new operator?