Please see the code example below
- Does anyone know why visual studio 22 behaves this way
- Does anyone know if there is a setting or something i can change to my debug configuration so that it does not generate the ‘not all paths return a value’ warning
Note: I have ‘treat warnings as errors’ – this cannot change
// In debug this compiles
// In release this has a compiler warning about unreachable code
int aaa()
{
int value = 0;
try
{
value = myFunctionThatCouldThrow();
}
catch (const std::exception& exc)
{
std::stringstream ss;
ss << "my function threw: " << exc.what();
throw std::exception(ss.str().c_str());
}
return value;
}
// In debug this has a compiler warning that not all paths return a value
// In release this compiles
int aaa()
{
try
{
return myFunctionThatCouldThrow();
}
catch (const std::exception& exc)
{
std::stringstream ss;
ss << "my function threw: " << exc.what();
throw std::exception(ss.str().c_str());
}
}
// This compiles in both but requires the preprocessor debug flag
int aaa()
{
try
{
return myFunctionThatCouldThrow();
}
catch (const std::exception& exc)
{
std::stringstream ss;
ss << "my function threw: " << exc.what();
throw std::exception(ss.str().c_str());
}
#ifdef _DEBUG
// The return statement is a pointless line as it can never be hit - I agree with the release build warning
// yet without this return statement - code building in debug generates a warning...
// I know I could also pragma ignore the warning - Im hoping for a cleaner solution without macros
// There are multiple functions in the code base that behave this way (the codes been ported from VS17)
// A non macro solution is better as I dont need to amend all functions
// I also dont want a more global ignore of the warning as that could hide genuine issues
return -1;
#endif
}
2