2

I am getting the following exception in my C#.Net program:

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll First-chance exception at 0x000007feff75121b in myapp.exe: 0xC0000005: Access violation reading location 0x0000000000000000. A first chance exception of type 'System.AccessViolationException' occurred in CustomMarshalers.dll An unhandled exception of type 'System.AccessViolationException' occurred in CustomMarshalers.dll Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I get it inside of a try/catch(Exception) block but it is never being caught.

I'm trying to debug it to see why it is happening but it is difficult to do that without catching it.

Should it be possible to catch a 0xC0000005 exception in C#.Net? (Framework 4.0 on Windows 2008 R2).

Thanks for any tips.

Neil Weicher
  • 2,370
  • 6
  • 34
  • 56
  • The exception is an `SqlException`, probably caused by trying to dereference a null pointer. – Lee Jun 15 '12 at 18:16
  • Its a Access violation exception. Check this reply: http://stackoverflow.com/questions/3469368/how-to-handle-accessviolationexception – ata Jun 15 '12 at 18:19

1 Answers1

11

You cannot catch an AccessViolationException by default, as it indicates a try to read or write protected memory. You can add the [HandleProcessCorruptedStateExceptions] attribute to the method where it occurs, though:

By default, the common language runtime (CLR) does not deliver these exceptions to managed code, and the try/*catch* blocks (and other exception-handling clauses) are not invoked for them. If you are absolutely sure that you want to maintain your handling of these exceptions, you must apply the HandleProcessCorruptedStateExceptionsAttribute attribute to the method whose exception-handling clauses you want to execute. The CLR delivers the corrupted process state exception to applicable exception clauses only in methods that have both the HandleProcessCorruptedStateExceptionsAttribute and SecurityCriticalAttribute attributes.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • 1
    This answer is very useful, thank you. I know it was a long time ago, but when you say "if you are absolutely sure you want to handle these exceptions", is there any disadvantage to performance of adding the [HandleProcessCorruptedStateExceptions] attribute? – Greg Nov 17 '21 at 21:55