Unreachable code error for exception handling code

S

SteadySteps

Hi

I migrated a project which compiles correctly on VC 6.0 to VS 2002. However
now all I get several warning that all the statements within catch blocks
are "unreachable code". How can I correct this ? C++ exceptions are enabled
( /EHsc ).

Thanks.
SS
 
C

Carl Daniel [VC++ MVP]

SteadySteps said:
Hi

I migrated a project which compiles correctly on VC 6.0 to VS 2002.
However now all I get several warning that all the statements within
catch blocks are "unreachable code". How can I correct this ? C++
exceptions are enabled ( /EHsc ).

Can you narrow down a simple repro case that you can post here that produces
the warnings you're seeing, and the command-line options you're using to
compile it?

-cd
 
S

SteadySteps

Hi

The project is an ATL Server project. I have now discovered that the
warnings occur only in the debug mode - so the compiler should not be
removing any code.

The switches used are:

/Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS"
/D "_AFXDLL" /FD /EHsc /MDd /GR /Yu"stdafx.h" /Fp".\Debug/ProjectName.pch"
/Fo".\Debug/" /Fd".\Debug/" /W4 /nologo /c /ZI

The code is along these lines:

{
AFX_MANAGE_STATE(AfxGetAppModuleState())
try
{
m_dMemberSlope = newVal; //or some such simple assignments
}
catch( const _com_error& e )
{
ATLTRACE( _T("Error in line %d of %s: %s\n"), __LINE__, __FILE__,
e.ErrorMessage() );
ASSERT( false );
return e.Error();
}
catch( ... )
{
ATLTRACE( _T("Unknown error in line %d of %s\n"), __LINE__, __FILE__ );
ASSERT( false );
return E_FAIL;
}
return S_OK;
}

Thanks.
SS
 
C

Carl Daniel [VC++ MVP]

SteadySteps said:
Hi

The project is an ATL Server project. I have now discovered that the
warnings occur only in the debug mode - so the compiler should not be
removing any code.

The switches used are:

/Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS"
/D "_AFXDLL" /FD /EHsc /MDd /GR /Yu"stdafx.h"
/Fp".\Debug/ProjectName.pch" /Fo".\Debug/" /Fd".\Debug/" /W4 /nologo
/c /ZI

The code is along these lines:

It looks like the catch blocks could simply be deleted (and the try) - the
code in the try can't possibly throw those exceptions, so the catch is
unreachable - that's why the warning is appearing. In a release build, the
compiler will completely optimize away the try/catch, so you might simply
want to use #pragma warning (disable:4702) to suppress the warning.

-cd
 
S

SteadySteps

Yes that seems to be the case. The exception code is generally always added
in a kind of 'company' policy.

Thanks for you help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top