I have compiled the following code in Visual C++ Express 9.0.21022.8. I use Warning Level 4 and I have C++ exceptions enabled (/EHsc)
void ThrowingFunction()
{
throw SomeException();
}
int OtherFunction()
{
ThrowingFunction();
return 0; //C4702
}
In release the code generates warning "C4702 unreachable code" for the return statement. If I remove that line, I get a warning in debug ("C4715 not all control paths return a value"). Since I'm treating warnings as errors, this is a problem :) I can see why the 'unreachable code' warning is triggered: if the compiler inlines ThrowingFunction the result would be
int OtherFunction()
{
throw SomeException();
return 0; //C4702
}
But I don't think the result of optimisations should be my problem... Or am I missing something here?
The above example is obviously a simplification. Here is my actual code:
void ThrowFailedAPICallException(const TString& i_message_base)
{
TString error_message = i_message_base + L": " + GetLastErrorMessage();
throw FailedAPICallException(error_message);
}
AutoHandle OpenProcessToken()
{
HANDLE h_token;
if (OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &h_token) == TRUE)
return AutoHandle(h_token);
ThrowFailedAPICallException(L"Failed to open process token");
return AutoHandle();
}