Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

CFileException problem

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 423
Lambda for random numbers problem 7 116
IdTCPClient1->Disconnect(); not working 3 76
How to split this in C++ 4 106
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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

839 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