Solved

CFileException problem

Posted on 2001-09-13
4
1,220 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
...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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now