?
Solved

tryin to GetErrorMessage(...) from CFileException cause an assertion.

Posted on 2003-03-27
5
Medium Priority
?
1,887 Views
Last Modified: 2007-12-19
catch(CFileException  *ex)
{
     TCHAR   szCause[255];          
     ex->GetErrorMessage(szCause,255);
        ex->Delete();
}

when the program enter the catch block and try to execute GetErrorMessage then an exception occur which cause a popup message "Debug assertion failed!" to appear.
I am using a console application project with MFC in DLL.

0
Comment
Question by:yaroni188
[X]
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
  • 2
5 Comments
 
LVL 5

Expert Comment

by:Kocil
ID: 8216364
catch(CFileException  ex)
{
    TCHAR   szCause[255];          
    ex.GetErrorMessage(szCause,255);
}
0
 
LVL 5

Accepted Solution

by:
Kocil earned 760 total points
ID: 8216390
Sorry, I though it was Java :)
here it is for C++

catch(CFileException  *ex)
{
    TCHAR   szCause[255];          
    ex->GetErrorMessage(szCause,254);
    ex->Delete();
}
0
 

Expert Comment

by:RattleSnake
ID: 8216435
If nothing helps, you might want to try this wrapper for FormatMessage I coded a while ago and pass the error code to it. Not sure if this would solve your problem:

CString GetLastErrorMessage(DWORD dwErr)
{
    try
    {
        LPVOID lpMsgBuf;

        DWORD dwError   =  dwErr ? dwErr : GetLastError();

        DWORD dwFlags   =  FORMAT_MESSAGE_ALLOCATE_BUFFER    |
                           FORMAT_MESSAGE_FROM_SYSTEM        |
                           FORMAT_MESSAGE_IGNORE_INSERTS;

        DWORD dwLang    =  MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT);

        DWORD dwBytes   =  FormatMessage(dwFlags,NULL,dwError,dwLang,
                             (LPTSTR) &lpMsgBuf, 0, NULL);

        if (dwBytes == 0) {
            return CString("Unknown");
        }

        CString sMessage((char*)lpMsgBuf);
        LocalFree(lpMsgBuf);
        return sMessage;
    }

    catch (...)
    {
        return CString("Unknown");
    }
}


And then:

catch(CFileException *e)
{
    CString err = GetLastErrorMessage(e->m_lOsError);
}

0
 
LVL 9

Expert Comment

by:tinchos
ID: 9551248

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: Kocil

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

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