Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"warning C4702: unreachable code" in release mode only

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
OK, this makes absolutely no sense to me...
In release mode only, I get "warning C4702: unreachable code" on the 'throw' in the following piece of code:

if ( this->SetScore( *(rightSide.Score()) ) == false )
{
std::string strErr( "Error allocating memory in operator=() File: " );
strErr += __FILE__;
strErr += ", Line: ";
char szLine[16];
itoa( __LINE__, szLine, 10 );
strErr += szLine;
throw std::bad_alloc( strErr.c_str() ); // Warning: C4702
}

But, if I remove everything except the throw it works fine:

if ( this->SetScore( *(rightSide.Score()) ) == false )
{
throw std::bad_alloc( "test" );
}

The other way to stop the warning is to use:
#pragma optimize( "g", off )

I have another piece of code almost identical to the one's causing the error, but inside a template and there is no warning generated on those throw lines.

I can't see anything wrong Is this a bug in Visual Studio 6.0? I'm using SP5.

Thanks,
Chris.
 
Hey,

I wouldn't worry too much about the warning. Though, I can't see why it fires if you take out everything but the throw. Have you tried commenting out only one line at a time, to see if it is actually being generated by one of the above commands?

Either way, as long as the code actually works, don't sweat it. You could just run it in debug mode and watch the line by line until it gets there and make sure it is actually working. I had the same warning in a C# program, but it didn't actually interfere with anything.

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
These messages are on warning level 4 (max) only. It seems it's (pragmatically incorrect;) compiler reaction on the string strErr destructor call at the end of if block (unreachable after throw;). Of course, exception stack bootstrap mechanics do it, but VC++ 6.0 SP5 code generator doesn't know about it...
As usually, project warning level 3 is OK (no such warnings;)...
 
Thanks, I came to the same conclusion.
After I made the string static it stopped the warning since the destructor is no longer called...
I just created a function with a static string that takes the error message, __FILE__, and __LINE__ and combines them into one string which gets returned.

Example:

Code:
throw std::bad_alloc( StrErr( "Error Msg", __FILE__, __LINE__ ) );
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top