Discussion:
Use CStdioFile to throw CFileException object
(too old to reply)
alan
2003-11-07 07:25:20 UTC
Permalink
Dear all,

I am a newbie in mfc.
I have a problem on throw CFileException object.
I wrote a function to open a test.txt file from C drive.
if the file is existed, it works fine. But I can to catch error if
file doesn't exist. I don't know the following fucntion doesn't work.
I read MSDN , CStdioFile class will throw error if file the file
cannot be opened or created.

Can any body help me? I am very appreciated if any help.

void readFile(CString file)
{
CString str;
vector<CString> v;

try
{
CStdioFile f1("C:\\test.txt", CFile::modeRead);
f1.Close();
}
catch(CFileException e)
{
e.m_cause;
}

}


regards,
alan
Rob
2003-11-07 11:43:31 UTC
Permalink
Maybe you could use CFileFind to see if the file exists before you open it.
Look up CFileException in MSDN and click on the class member m_cause to
understand what the codes mean

e.g.
if( e->m_cause == CFileException::fileNotFound )
printf( "ERROR: File not found\n");

Hope that helps,

Rob
Joseph M. Newcomer
2003-11-07 16:56:32 UTC
Permalink
Generally, there is little point to using CFileFind in such a context. It can be handled
more simply by simply not specifying the filename in the constructor, e.g.,

CStdioFile f1;

if(!f1.Open("file.txt"))
{ /* failed */
DWORD err = ::GetLastError();
... use FormatMessage to get the actual text of the message,
... do not use literal printf strings!
return FALSE; // or whatever you want to do to indicate the error
}

Here's a nice function for that purpose:. ErrorString.h simply declares this function,
e.g.,
CString ErrorString(DWORD err);

================================================================
#include "stdafx.h"

#include "resource.h"
#include "ErrorString.h"


/****************************************************************************
* ErrorString
* Inputs:
* DWORD err: Error code
* Result: CString
* String message
****************************************************************************/

CString ErrorString(DWORD err)
{
CString Error;
LPTSTR s;
if(::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
err,
0,
(LPTSTR)&s,
0,
NULL) == 0)
{ /* failed */
// Unknown error code %08x (%d)
CString fmt;
CString t;
fmt.LoadString(IDS_UNKNOWN_ERROR);
t.Format(fmt, err, LOWORD(err));
Error = t;
} /* failed */
else
{ /* success */
LPTSTR p = _tcsrchr(s, _T('\r'));
if(p != NULL)
{ /* lose CRLF */
*p = _T('\0');
} /* lose CRLF */
Error = s;
::LocalFree(s);
} /* success */
return Error;
} // ErrorString
Post by Rob
Maybe you could use CFileFind to see if the file exists before you open it.
Look up CFileException in MSDN and click on the class member m_cause to
understand what the codes mean
e.g.
if( e->m_cause == CFileException::fileNotFound )
printf( "ERROR: File not found\n");
Hope that helps,
Rob
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Doug Harrison [MVP]
2003-11-08 05:22:14 UTC
Permalink
Post by alan
Dear all,
I am a newbie in mfc.
I have a problem on throw CFileException object.
I wrote a function to open a test.txt file from C drive.
if the file is existed, it works fine. But I can to catch error if
file doesn't exist. I don't know the following fucntion doesn't work.
I read MSDN , CStdioFile class will throw error if file the file
cannot be opened or created.
Can any body help me? I am very appreciated if any help.
void readFile(CString file)
{
CString str;
vector<CString> v;
try
{
CStdioFile f1("C:\\test.txt", CFile::modeRead);
f1.Close();
}
catch(CFileException e)
{
e.m_cause;
}
}
MFC only ever throws pointers. The proper way to catch an MFC exception is
like this:

catch (CFileException* e)
{
// Do stuff with e. If you're finished with it and don't rethrow it,
// you must call the Delete member.
e->Delete();
}

This is all pretty well spelled out here and in the related documentation:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_Exceptions.3a_.Catching_and_Deleting_Exceptions.asp?frame=true
--
Doug Harrison
Microsoft MVP - Visual C++
Vinay
2003-11-17 11:30:14 UTC
Permalink
use TRY and CATCH ( its different than try and catch)

Example code

TRY
{
CStdioFile f1("C:\\test.txt", CFile::modeRead);



}
CATCH(CFileException, pEx)
{
// Simply show an error message to the user.

pEx->ReportError();
}
AND_CATCH(CMemoryException, pEx)
{
// We can't recover from this memory exception, so we'll
// just terminate the app without any cleanup. Normally, an
// an application should do everything it possibly can to
// clean up properly and _not_ call AfxAbort().

AfxAbort();
}
END_CATCH

...

regds
vinay
Post by alan
Dear all,
I am a newbie in mfc.
I have a problem on throw CFileException object.
I wrote a function to open a test.txt file from C drive.
if the file is existed, it works fine. But I can to catch error if
file doesn't exist. I don't know the following fucntion doesn't work.
I read MSDN , CStdioFile class will throw error if file the file
cannot be opened or created.
Can any body help me? I am very appreciated if any help.
void readFile(CString file)
{
CString str;
vector<CString> v;
try
{
CStdioFile f1("C:\\test.txt", CFile::modeRead);
f1.Close();
}
catch(CFileException e)
{
e.m_cause;
}
}
regards,
alan
Bill Swartz
2003-11-17 15:42:56 UTC
Permalink
Have you tried to catch a CFileException * pe?
Bill
Post by Vinay
use TRY and CATCH ( its different than try and catch)
Example code
TRY
{
CStdioFile f1("C:\\test.txt", CFile::modeRead);
}
CATCH(CFileException, pEx)
{
// Simply show an error message to the user.
pEx->ReportError();
}
AND_CATCH(CMemoryException, pEx)
{
// We can't recover from this memory exception, so we'll
// just terminate the app without any cleanup. Normally, an
// an application should do everything it possibly can to
// clean up properly and _not_ call AfxAbort().
AfxAbort();
}
END_CATCH
...
regds
vinay
Post by alan
Dear all,
I am a newbie in mfc.
I have a problem on throw CFileException object.
I wrote a function to open a test.txt file from C drive.
if the file is existed, it works fine. But I can to catch error if
file doesn't exist. I don't know the following fucntion doesn't work.
I read MSDN , CStdioFile class will throw error if file the file
cannot be opened or created.
Can any body help me? I am very appreciated if any help.
void readFile(CString file)
{
CString str;
vector<CString> v;
try
{
CStdioFile f1("C:\\test.txt", CFile::modeRead);
f1.Close();
}
catch(CFileException e)
{
e.m_cause;
}
}
regards,
alan
Loading...