This is kind of a follow-up question to my previous question Why is my exception still being thrown after being caught?
I have a reference data member r
of class S
which attempts to be initialized with the result of the function call f
. f
throws so I have set up a try-catch block. I don't expect r
to be given a value as the exception is thrown before the function returns. But when I print out r
in the catch block I get 1
as a result.
#include <iostream>
#include <stdexcept>
int& f() { throw std::invalid_argument("invalid"); static int i(50); return i; }
struct S
{
S()
try : r(f())
{ }
catch(...)
{
std::cout << r;
}
int& r;
};
int main()
{
try { S s; } catch(...) { }
}
Output:
1
I don't know why I am getting 1 as opposed to a random value or 0 like a regular variable. Is a result of 1 mandated by the Standard or is this just Undefined Behavior? I would really like to know. Thanks.