Exceptions not caught  
Author Message
Simon Heffer





PostPosted: Visual C++ General, Exceptions not caught Top

Using Visual Studio 2005 C++ using MFC.
I've isolated my problem to this:
BOOL CMyApp::InitInstance()
{
int z=0;
int y,x=8;

try
{
//y=x/z; // divide by 0
char *p=NULL;
*p='l'; // access violation
}
catch(CException *e)
{
AfxMessageBox("Exception!");
}
...

When I run this under the VS 2005 de**** I get and unhandled exception.alert Am I missing some feature I need to turn on


Visual C++10  
 
 
Mike Danes





PostPosted: Visual C++ General, Exceptions not caught Top

Exceptions like "access violation" are not standard C++ exceptions, they don't have a type and so the only way to catch them is to use catch (...). In addition to enable catching of such exceptions (asynchronous exceptions) in C++ you need to change the Enable C++ Exceptions compiler option from /EHsc (the default) to /EHa (see Project Properties, Configuration Properties, C/C++, Code Generation).

See this MSDN article for more information (and a way to wrap this type of exceptions in C++ Exceptions):

http://msdn.microsoft.com/library/default.asp url=/library/en-us/vclang/html/_core_exception_handling_differences.asp


 
 
Simon Heffer





PostPosted: Visual C++ General, Exceptions not caught Top

Many thanks.

I also found http://support.microsoft.com/kb/167802 which seems to do the trick.