Re: Handing exceptions
On Mon, 25 Feb 2008 15:18:59 +0100, "Lisa Pearlson" <no@spam.plz> wrote:
Hi,
My debug output shows an exception when using CArchive::ReadString() beyond
the end of the file.
I thought that by handling exceptions, I could prevent the debug output for
them:
CFile f;
if (f.Open(_T("\\Temp\\test.txt"), CFile::modeRead)) {
CArchive ar(&f, CArchive::load);
CString s;
try {
while (ar.ReadString(s)) {
DEBUGMSG(1, (_T("ReadString: %s\r\n"), s));
}
} catch (CArchiveException* e) {
e->ReportError(); // never gets called!
e->Delete();
}
}
However, output is still shown for CArchiveException::endOfFile, and my
exception handler is never called.
Yes, I provided the /EHsc (and tried deprecated /GX) compiler options, set
via Project Settings/C++/Exception Handling in VS 2005.
Any clues?
The documentation says:
CArchive::ReadString
<q>
In the version that returns Bool, TRUE if successful; FALSE otherwise.
In the version that returns an LPTSTR, a pointer to the buffer containing
the text data; NULL if end-of-file was reached.
</q>
I don't know if a FALSE return is the same thing as a NULL return, but the
source code is provided, and I find:
CATCH(CArchiveException, e)
{
if (e && e->m_cause == CArchiveException::endOfFile)
{
DELETE_EXCEPTION(e);
if (nRead == 0)
return NULL;
}
else
{
THROW_LAST();
}
}
This is in the LPSTR-returning version, which the BOOL-returning version
uses. Therefore, the EOF exception occurs, including side-effects such as
your trace output, but MFC turns it into a return code when there is no
string data at all to read, and that's why you can't catch it.
--
Doug Harrison
Visual C++ MVP