Dear diary,
today exceptions bit me unusually hard. I am seriously considering filing a compiler bug report...
When thinking about exceptions, one would argue that any code that is after a "throw" statement will never get executed, right? Try the following code in MSVC2017 debug build:
#include <iostream>
using namespace std;
int main()
{
cout << "Start" << endl;
class TestException:
std::runtime_error
{
public:
TestException():
std::runtime_error("TestException")
{
}
};
try
{
throw TestException();
cout << "After throw" << endl;
}
catch (const std::runtime_error & exc)
{
cout << "Caught a runtime_error" << endl;
}
cout << "Done" << endl;
return 0;
}
So, what does it do in reality? It prints "Start", "After throw", and "Done", and exits with code 0. WTF?!? How can the "After throw" line be printed??
Let's try release build, then. That one depends whether you run it in the MSVC debugger, or standalone. In the debugger, it prints "Start", "After throw" and jumps into nonexistent memory. WTF again? Standalone, it finally behaves as one would expect: prints "Start" and crashes.
I've spent a good hour hunting this bug. Turns out, the only thing I forgot in the code above is the "public" inheritance keyword in TestException's declaration. But still, without it the code behaves just weird.