Mind the following code:
struct exception_in_copy_constructor
{
exception_in_copy_constructor() = default;
~exception_in_copy_constructor() = default;
exception_in_copy_constructor(const exception_in_copy_constructor& other)
{
throw std::exception(":-(");
}
};
Now, it is clear that when I throw this object, an std::exception will be thrown.
So lets look at the following code:
try
{
exception_in_copy_constructor ecc;
throw ecc;
}
catch(exception_in_copy_constructor& ecc)
{
printf("IN exception_in_copy_constructor&\r\n");
}
catch(std::exception& ex)
{
printf("IN std::exception&\r\n");
}
I would expect the before ecc would be thrown, an std::exception will be thrown, and because of that, ecc will never be thrown. The code above confirmed that by printing "IN std::exception&"
BUT mind this code:
try
{
exception_in_copy_constructor ecc;
throw ecc;
}
catch(exception_in_copy_constructor& ecc)
{
printf("IN exception_in_copy_constructor&\r\n");
}
I would expect that nothing will get caught, but to my surprise, catch(exception_in_copy_constructor& ecc) caught the exception (?!?!)
Can anyone explain what is going on?
I am using VS2015
Thanks!
EDIT: Users wrote they cannot reproduce, so I am writing the whole code as it is on my computer. Also, I am using VS2015, debug, x64 (It also reproduce in release).
Here is the code:
int main()
{
struct exception_in_copy_constructor
{
exception_in_copy_constructor() = default;
~exception_in_copy_constructor() = default;
exception_in_copy_constructor(const exception_in_copy_constructor& other)
{
throw std::exception(":-(");
}
};
try
{
exception_in_copy_constructor ecc;
throw ecc;
}
catch(exception_in_copy_constructor&)
{
printf("We will never get here!\r\n");
}
return 0;
}