Solved

CFileException problem

Posted on 2001-09-13
4
1,244 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 100 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

777 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