Exception handling?
In my below foobar first attempt to try exception handling I don't understand
the following aspects: (my code at bottom after these framework pastes)
1. I must not be doing something correct because even though both
my CATCH and AND_CATCH call stuff on return that appears to be
deleting the exception and taking care of various cleanup.
// abbreviated Step thru pastes
//---------------------------------------
void AFXAPI AfxTryCleanup( )
{
......
.....
// delete current exception
ASSERT(pLinkTop != NULL);
if (pLinkTop->m_pException != NULL)
pLinkTop->m_pException->Delete( );
.....
.....
}
// and later
CString::~CString()
// free any attached data
{
....etc etc
}
//--------------------------------------
* * when the exception returns back to document caller I don't see this
* * getting executed,
if (!pDocument->OnNewDocument())
{
// user has been alerted to what failed in OnNewDocument
TRACE0("CDocument::OnNewDocument returned FALSE.\n");
if (bCreated)
pFrame->DestroyWindow(); // will destroy document
return NULL;
}
.....
.......
// Instead I see this down the road,
CDocument* CWinApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
ASSERT(m_pDocManager != NULL);
//* *And lpszPathName still = TheFileNameItriedToOpen ? * *
return m_pDocManager->OpenDocumentFile(lpszFileName);
}
//* *
So if I save my document after code turns app back over to me I overwrite
the file that I could not open ? So there must be more " I " have to do, or something
different if I have it all wrong out of the shute.
2. Also from the docs I have read I got the impression that by putting THROW_LAST( );
at the end of my CATCH( CUserException, e ) that it would forward the exception
to my AND_CATCH( CException, e ) but experimentation shows that does not
happen, so I missed something there. But anyhow both exceptions seem to pass
to clean up code on return so question 2 is only a curiousity. Question 1 is my
main concern.
=== foobar attempt below ====
void CFileHandlingDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << FileID; // DWORD
ar << VerData.Ver << VerData.CpyRt << VerData.Corp;
ar.SerializeClass(RUNTIME_CLASS(CMapStringToString));
ExpMap1.Serialize(ar);
}
else
{
CString E;
TCHAR szErrMsg[255];
TRY
{
ar >> FileID;
if (FileID != 0x1234ABCD)
{ // FileID mismatch
AfxThrowUserException( );
}
ar >> VerData.Ver >> VerData.CpyRt >> VerData.Corp;
ar.SerializeClass(RUNTIME_CLASS(CMapStringToString)); // WriteClass CmStS's data
ExpMap1.Serialize(ar); // CMapStringToString's keys and values
}
CATCH( CUserException, e )
{
E = _T("BAD FileID, RB from inside CATCH\n");
AfxMessageBox( E );
return;
}
AND_CATCH( CException, e )
{
// For other exception types, notify user here.
e->GetErrorMessage(szErrMsg, 255);
E = _T("RB from inside AND_CATCH\n");
E += szErrMsg;
AfxMessageBox( E );
return;
}
END_CATCH
// No exception thrown.
}
}