Exception caught but not handled completely
-
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 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::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
-
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 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::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
All the code in the exception handler should be able to execute outside the scope of the try block. One glaringly obvious potential you have is the use of
pDataMovement
pointer which is initialized in the try block. So if the exception is thrown before then or if the pointer is null your catch block will just throw another exception. Wait, looking again I see that pointer used before it is even declared so I don't know how you get that to compile at all.led mike