C++ Quiz113 of 173
Difficulty:
⬅️ Prev: 238Quiz ID: 239Next: 241 ➡️

According to the C++23 standard, what is the output of this program?

#include <stdexcept>
#include <exception>
#include <iostream>

int main() {
    try {
        throw std::out_of_range("");
    } catch (std::exception& e) {
        std::cout << 1;
    } catch (std::out_of_range& e) {
        std::cout << 2;
    }
}
Hint

The handlers for a try block are tried in order of appearance.

Reveal answer

1

Explanation

There are two exception handlers, exception& e and out_of_range& e. Both would be appropriate handlers for an out_of_range exception, since out_of_range inherits from exception.

How is the handler selected? §[except.handle]¶4:

The handlers for a try block are tried in order of appearance.

[Note 2: This makes it possible to write handlers that can never be executed, for example by placing a handler for a final derived class after a handler for a corresponding unambiguous public base class. — end note]

The note in the standard hints at what's happening here: Even if out_of_range& e is a more specific match, exception& e matches before we even get to try it, and 1 is printed.

⬅️ Prev: 238Quiz ID: 239Next: 241 ➡️