[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

CFileException problem

Posted on 2001-09-13
4
Medium Priority
?
1,339 Views
Last Modified: 2012-08-14
Hello all,
   I have the following code (snippet)

...
CFile szTemplateFile;
CFileException* pFileError;
CString ErrorText;

if (! szTemplateFile.Open(m_szTemplateParameterData, CFile::modeRead | CFile::shareDenyWrite, pFileError))
{
     FileErr.Clear();
     ErrorText = "Can not open file " + m_szTemplateParameterData;
     FileErr.m_lpszErrorDescription  = ErrorText;
     FileErr.m_nErrorCode = pFileError->m_cause;
...

When I pass a valid file name into the function I don't have any problems.  When I pass an invalid file name into the function I get an unhandled exception error, and I find that pFileError has no information in it.  Any ideas as to what I am doing wrong?

Thanks



0
Comment
Question by:edc
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6480885
You don't catch the exception - use

TRY {
if (! szTemplateFile.Open(m_szTemplateParameterData, CFile::modeRead | CFile::shareDenyWrite, pFileError)) {
// error
}
CATCH( CFileException* pFileError){
    FileErr.Clear();
    ErrorText = "Can not open file " + m_szTemplateParameterData;
    FileErr.m_lpszErrorDescription  = ErrorText;
    FileErr.m_nErrorCode = pFileError->m_cause;
}
0
 
LVL 1

Author Comment

by:edc
ID: 6480910
Thanks for the reply jkr.  I found that if I declare CFileException pFileException rather than CFileException* pFileException and then pass the address to the CFile::Open function that it works, although it would be nice to know why.  I will try your suggestion as well.  It never hurts to have more than one way of doing things.
0
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 6480924
Ooops, you're correct - didn't see that, as exceptions are usually handled the try/catch way. But, the MS docs also use

//example for CFile::Open
CFile f;
CFileException e;
char* pFileName = "test.dat";
if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e ) )
   {
#ifdef _DEBUG
   afxDump << "File could not be opened " << e.m_cause << "\n";
#endif
   }

0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6481204
...although it would be nice to know why.

The reason is that in this line,

   CFileException* pFileError;

you declare pFileError as a pointer, but it doesn't point to anything.  If you want to use it that way, you could have used:

CFileException* pFileError= new CFileException;

Thuse, in the call to

.... Open(..., pFileError)...

you were passing an invalid pointer in to the Open call.  That caused an unhandled exception when the system tried to write some data to that address.  I'm thinkijng that the CFileException that is handled by MFC's Open fn handles regular, file-related exceptions, but not memory access exceptions.

As jkr says, the normal way to handle this is something like ...

CFileException e;
BOOL fRet=  .... Open(..., &e );
if (fRet==FALSE ) { // ERROR!!!
      char buf[256];
      e.GetErrorText( buf, sizeof(buf) );
      AfxMessageBox( buf );
}
 
-- Dan
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question