So I have VS 2017, created New MFC Application, compiled Release x86 build with /clr, Multibyte character set, Optimized favor speed (/O2).
When ::Run function contains __try __except exception handler, the function will not be called in optimized release build, or throws error "Common Language Runtime detected an invalid program.".
When using try-catch or if C/C++ Optimization Disabled (/Od), it works as expected.
int CMFCTryExceptTestApp::Run()
{
::AfxMessageBox("CMFCTryExceptTestApp::Run");
for (;;)
{
__try
{
int ret = CWinAppEx::Run();
return ret;
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
ASSERT(FALSE);
}
}
}
This works always:
int CMFCTryExceptTestApp::Run()
{
::AfxMessageBox("CMFCTryExceptTestApp::Run");
for (;;)
{
try
{
int ret = CWinAppEx::Run();
return ret;
}
catch(...)
{
ASSERT(FALSE);
}
}
}
I have been also using __try __except elsewhere in the program and it works there.