views:

38

answers:

1

I got this exception in my program:

Unhandled exception at 0x0051cce0 in JSONDataParsing.exe: 0xC0000005: Access violation reading location 0x00000004

.

I tried catching the Exception, but of no use. I know where the problem occurs. But wanted to know how I can trap the exception. I used try, catch block around the code where exception occurs.

Is this an exception which can not be caught?

The catch statements are:

catch (bad_alloc&)
        {
            TCHAR msgbuf[MAX_PATH];

            swprintf(msgbuf, L"bad_alloc \n");

            OutputDebugString(msgbuf);
        }
        catch (bad_cast&)
        {
            TCHAR msgbuf[MAX_PATH];

            swprintf(msgbuf, L"bad_cast \n");

            OutputDebugString(msgbuf);
        }
        catch (bad_exception&)
        {
            TCHAR msgbuf[MAX_PATH];

            swprintf(msgbuf, L"babad_exceptiond_alloc \n");

            OutputDebugString(msgbuf);
        }
        catch (bad_typeid&)
        {
            TCHAR msgbuf[MAX_PATH];

            swprintf(msgbuf, L"bad_alloc \n");

            OutputDebugString(msgbuf);
        }
        catch( CMemoryException* e )
        {

            TCHAR msgbuf[MAX_PATH];

            swprintf(msgbuf, L"CMemoryException \n");

            OutputDebugString(msgbuf);
            // Handle the out-of-memory exception here.
        }


        catch( CFileException* e )
        {

            TCHAR msgbuf[MAX_PATH];

            swprintf(msgbuf, L"CFileException \n");

            OutputDebugString(msgbuf);
            // Handle the file exceptions here.
        }

        catch( CException* e )
        {

            TCHAR msgbuf[MAX_PATH];

            swprintf(msgbuf, L"CException \n");

            OutputDebugString(msgbuf);
            // Handle the exception here.
            // "e" contains information about the exception.
            e->Delete();
        }
+1  A: 

You can only catch such exception with a special try-catch handler:

try
{
  // code that triggers such an exception. for example:
  int * a = NULL;
  *a = 0;
}
catch (...)
{
  // now that exception is handled here
}

But generally, it is bad practice to do this. Instead you should not get such an exception but check your parameters and variables.

See here for more details: http://members.cox.net/doug_web/eh.htm

Stefan
Hi Stefen,I used
Krishnan
Hi Stefan, I used this and could not trap the access violation.
Krishnan
you have to compile with the /EHa flag
Stefan