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();
        }
RK-
  • 12,099
  • 23
  • 89
  • 155
  • Please show us your `catch` statement – mmonem Sep 23 '10 at 17:04
  • 1
    does `catch (...)` work? Also check out [What should I know about Structured Exceptions (SEH) in C++?](http://stackoverflow.com/questions/2782915/what-should-i-know-about-structured-exceptions-seh-in-c) – Nick Dandoulakis Sep 23 '10 at 17:12

2 Answers2

1

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
  • 43,293
  • 10
  • 75
  • 117
0

This type of low level exception can be caught using __try -except statement, but you should fix the cause of it rather than reporting it. In VS, while debugging hit CTRL+ALT+E and check all exceptions there, then continue to run your app until the exception occurs. The prompt will stop on the offending line. See http://msdn.microsoft.com/en-us/library/s58ftw19.aspx for details.

Pifcnt
  • 117
  • 1
  • 10