Click here to Skip to main content
15,891,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have a code snippet as follows

C++
struct ItemNode
{
    CString szID;
};

void DoSomething()
{
    ItemNode *a = NULL;

    TRY {
    CString s;
    s.Format (_T("%s"), a->szID);
    }
    CATCH(CException b){
    MessageBox (_T("Exception occured"));
    b->Delete ();
    }
    END_CATCH
    return;
}


Here, I am not able to catch the exception. The app crashes saying "There is an access violation.... "
What is the correct way to catch the exception??
I am using MFC, C++ and the development environment is in VisualStudio 2005.

EDIT: Cross posting of http://www.codeproject.com/Forums/1647/C-Cplusplus-MFC.aspx?fid=1647&select=3486093&tid=3486093[^]
Posted
Updated 27-May-10 23:36pm
v3

You're not catching an exception because there's not a C++ exception to catch.

What's happening is that you're dereferencing a zero pointer. This raises an operating system exception, NOT a C++ one. There are ways with VC++ that you can convert one to the other (look up _se_set_translator in MSDN) but as you're not doing them there's no C++ exception.

A couple of other points...

- As far as I know CString::Format doesn't throw a CException if it fails so the CATCH is a bit pointless

- You don't have to use the MFC 1.x TRY/CATCH rubbish anymore - you can be less shouty and use try/catch instead

- If you do use the MFC TRY/CATCH macros you don't have to do the b->delete thing.

- Did your code compile as there's not a comma between CException and b?

- If you don't use the TRY/CATCH macros and catch an MFC exception you have to do the b->Delete() thing as MFC doesn't follow the best practice of throwing by value and catching by reference

Hope that helps,

Cheers,

Ash
 
Share this answer
 
In addition to Aescleal's answer I would say that access violations are a serious problem and should not be caught. You should instead make your code work properly.
If you still want to catch access violations than you can use the Microsoft specific __try/__except[^] or see this[^] article to catch access violations using standard C++ try/catch[^]. :)
 
Share this answer
 
Dereferencing a NULL pointer will cause the CPU to throw an hardware exception that usually is handled by the operating system, and generally is not a good programming style to catch this kind of exception, instead the best is to test conditions before executing code that could fall in such an exception.
However, if you want to catch this kind of exception, you could do it in three ways (all are Microsoft specific features):




  1. translate Win32 exceptions to C++ exceptions using _set_se_translator and the C++ try/catch construct (see http://msdn.microsoft.com/en-us/library/5z4bw5h5(VS.80).aspx[^]); note that you must compile your code with the /EHa switch to make this method working
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900