If a constructor throws after its members were initialised, what is destroyed?
The object never finished being constructed, so it never existed.
Exception Handling · Exercise 1 of 4
Twenty multiple-choice questions on C++ exception handling: try/catch/throw, the standard exception hierarchy, catch order, rethrowing, stack unwinding, noexcept, constructors, destructors and exception safety. Pick an option to check your answer instantly.
If a constructor throws after its members were initialised, what is destroyed?
The object never finished being constructed, so it never existed.
A constructor does buf_ = new char[1024]; and then calls a function that throws. What happens to the buffer?
Members that manage themselves are destroyed during the failed construction.
Which is the best fit for std::logic_error rather than std::runtime_error?
logic_error marks bugs that could have been prevented; runtime_error marks conditions outside the program's control.
What is std::exception::what()?
Its signature is virtual const char* what() const noexcept.
What does the strong exception guarantee promise?
The basic guarantee only promises no leaks and valid (but possibly changed) objects.
What is wrong with throw new std::runtime_error("x");?
Throw by value; the runtime manages the exception object's lifetime.
What is a typical use of std::exception_ptr?
std::future and std::async use it to deliver exceptions to the thread that calls get().
Why use a function-try-block on a constructor: Widget() try : member(f()) { } catch (…) { }?
A constructor cannot "recover" from a failed member: the object is not usable.
Which situation suits std::optional/std::expected better than throwing?
"Not found" is a normal, frequent outcome – not exceptional.
What does the expression noexcept(f()) evaluate to?
f is not actually called – the operand is unevaluated.
Timed online tests with instant scores and explanations.