C++ Quiz154 of 173
Difficulty:
⬅️ Prev: 319Quiz ID: 322Next: 323 ➡️

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

#include <iostream>
#include <stdexcept>

int main()
{
    goto inside;
    try
    {
        inside:
        throw std::runtime_error("oops");
    }
    catch(...)
    {
        std::cout << 1;
    }
    std::cout << 2;
}
Hint

This might make it a bit hard for some implementations to set up exception handling?

Reveal answer

The program has a compilation error

Explanation

This is not legal. According to §[except.pre]¶3 in the standard:

The compound-statement of a try block or of a handler is a control-flow-limited statement (§[stmt.label]).

and via §[stmt.label]¶3:

A control-flow-limited statement is a statement S for which:

— (...)

— a label declared in S shall only be referred to by a statement (§[stmt.goto]) in S.

So this fails to compile.

⬅️ Prev: 319Quiz ID: 322Next: 323 ➡️