Back to questions
JuniorTheoryCommon
What does the program print?
#include <iostream>
struct Error {};
struct NotFound : public Error {};
int main() {
try { throw NotFound{}; }
catch (const Error&) { std::cout << "Error"; }
catch (const NotFound&) { std::cout << "NotFound"; }
}Error
- Expecting
NotFound: handlers are tried from top to bottom, not by the best match, andconst Error&already accepts aNotFound. - Expecting a compile error: a handler that can never run draws only a warning, and the program builds.
- How would you reorder the handlers so that a
NotFoundgets its own message? - Where in the list of handlers does
catch (...)go, and why?