Click to See Complete Forum and Search --> : Exception not handled completely


RoyceF
May 20th, 2008, 03:20 PM
I have an exception handling problem that I don't understand. My thread function is throwing an unhandled exception which is being caught by my catch(...) handler but not completely handled. This causes my thread to be prematurely terminated so that my cleanup logic is not executed. How can I change my handler so as to completely handle the exception and execute my cleanup logic? My code is below.


UINT CVarFileEvent::MultiFileProcessThread( LPVOID lpParam )
{
// This is the threaded multi-file processor. It
// searches for and processes all VAR files in the
// Inbox or until it is cancelled by a call from
// DataMovement due to a Cancel message from PC-GBS.
CVarFileEvent* pEvent = (CVarFileEvent*)lpParam;
TRACE( "> > > > Entering CVarFileEvent::MultiFileProcessThread()\n" );

// We must call ::CoInitialize() each time the database is accessed
HRESULT hRes = ::CoInitialize(NULL);
try
{
vector<CString> vecVarFiles = UtilK::findAllFiles( "*.var",
pDataMovement->getInboxPath(),
false );
TRACE( "> > > > > Beginning processing of %d VAR files\n",
(int)vecVarFiles.size() );

CDataMovement* pDataMovement = pEvent->getDataMovementParent();
CFlightDataHandler FlightDataHandler( pDataMovement->getLogWriter() );
// run until we are cancelled or all files processed
for( vector<CString>::iterator iterFile = vecVarFiles.begin();
iterFile != vecVarFiles.end() && m_bIsThreadRunning;
iterFile++ )
{
FlightDataHandler.importVARFile( *iterFile );
// now delete the input file and remove the event
// for this file from the queue if it exists
::DeleteFile( *iterFile );
pDataMovement->deleteFileEvent( *iterFile );
}
}
catch(...)
{
CString strMsg( "importVARFile() failed with unexpected exception" );
TRACE( "> > > > CVarFileEvent::MultiFileProcessThread() exception < %s >\n", strMsg );
CLogWriter* pLogWriter = pDataMovement->getLogWriter();
pLogWriter->writeEntry( "CVarFileEvent", CLogWriter::LE_OPERATION_FAILED, strMsg );
}

// Cleanup
::CoUninitialize();
// the main thread is waiting so indicate we have stopped processing
pDataMovement->setInboxProcessEndEvent();
m_bIsThreadRunning = false;
return 1;
}




Thanks

a1ex07
May 20th, 2008, 04:35 PM
If your exception is really caught by catch(...) block, then it seems that another exception is raised inside this catch (...). You can add another try{}-catch {} into your original catch(...) block:

catch (...)
{
try
{
CString strMsg( "importVARFile() failed with unexpected exception" );
TRACE( "> > > > CVarFileEvent::MultiFileProcessThread() exception < %s >\n", strMsg );
if (CLogWriter* pLogWriter = pDataMovement->getLogWriter())
{
pLogWriter->writeEntry( "CVarFileEvent", CLogWriter::LE_OPERATION_FAILED, strMsg );
}
else
{
TRACE ("LogWriter not found");
}
}
catch (...)
{
TRACE("Unknown error inside catch block");
}
}

// Cleanup

If the code inside your catch block is never executed, make sure that /EHa option is set (Configuration->C/C++ ->Enable C++ Exceptions