Is there anything I can do to catch an AccessViolationException
? It is being thrown by a unmanaged DLL that I don't control.

- 160,644
- 26
- 247
- 397

- 68,373
- 70
- 259
- 447
5 Answers
You shouldn't. An access violation is a serious problem: it is an unexpected attempt to write to (or read from) an invalid memory address. As John already clarified, the unmanaged DLL might already have corrupted the process memory before the access violation has been raised. This can have unpredicted effects on any part of the current process.
The safest thing to do is to possibly inform the user and then immediately exit.
Some more details: An access violation is an OS exception (a so-called SEH or structured exception handling exception). This is a different kind of exception than the managed CLR exceptions from System.Exception
. You will rarely see SEH exceptions in purely managed code, but if one occurs, e.g. in unmanaged code, the CLR will deliver it to managed code where you are also able to catch it1.
However, catching SEH exceptions is mostly not a good idea. Further details are explained in the article Handling Corrupted State Exceptions in MSDN magazine where the following text it taken from:
The CLR has always delivered SEH exceptions to managed code using the same mechanisms as exceptions raised by the program itself. This isn't a problem as long as code doesn't attempt to handle exceptional conditions that it cannot reasonably handle. Most programs cannot safely continue execution after an access violation. Unfortunately, the CLR's exception handling model has always encouraged users to catch these serious errors by allowing programs to catch any exception at the top of the System.Exception hierarchy. But this is rarely the right thing to do.
1This was true until .NET 3.5. In .NET 4 the behavior has been changed. If you still want to be able to catch such kind of exceptions you would have to add legacyCorruptedStateExceptionsPolicy=true
to the app.config. Further details in the articled linked above.

- 172,527
- 53
- 255
- 316
-
6To clarify: the reason you want to exit ASAP is that you don't know what the unmanaged DLL overwrote before it hit the Access Violation. It may have been writing garbage in enough places that your program cannot safely continue. – John Saunders Jul 22 '10 at 18:38
-
8I can think of one very good reason to catch this: If you have an unmanned process and you don't have this, instead of exiting, the process will hang with the Access Violation dialog box showing nobody in particular. Catching this lets you exit without that dialog popping up. – jpwkeeper May 16 '13 at 14:09
-
6Even informing the user or logging the problem, then exiting, requires catching the exception. – Jonathan Allen Sep 17 '14 at 19:01
-
4Just so people know. An AccessViolationException doesn't necesarrilly mean a 'write'. It can be a 'read' as well. – WonderWorker Sep 28 '16 at 08:04
-
1To go with what Noggins said, there are good reasons for catching exceptions from C# just like there are in C itself, just like there are in C# from C#. There are lots of examples where an error in a service or utility DLL shouldn't take down the whole system. – Beeeaaar Jul 06 '17 at 18:39
As others pointed out, you shouldn't "handle" this condition, but during development it is handy to catch this for the sake of troubleshooting.
You can mark your managed method with the System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions
attribute:
[HandleProcessCorruptedStateExceptions]
public void MyMethod()
{
try
{
NaughtyCall();
}
catch (AccessViolationException e)
{
// You should really terminate your application here
}
}

- 3,892
- 17
- 31
-
1In a Context of an Applicationplugin for AutoCAD I never reach the catch, however, the host wont crash and this is sufficient for my usecase :) (I'm calling a method of the host-App's API where the error occures, and I'm not aware of any precheck to prevent...) – dba May 14 '19 at 13:44
Yes.
In your App.confg, plop the following code within the <configuration>
tag:
<runtime>
<legacyCorruptedStateExceptionsPolicy enabled="true"/>
</runtime>
Now you should be able to catch corrupted state exceptions (CSE) like any other.
Note: If you already have a runtime tag then simply add <legacyCorruptedStateExceptionsPolicy enabled="true"/>
to it
The above works for .Net 4.5

- 8,539
- 4
- 63
- 74
-
2th for this. I read a couple of answers to this issue. all saying "add `
` to `app.config`" but no one said that is should not be in the default ` ` section. Thanks for the hint with the ` – flor1an Jun 26 '17 at 19:34` section
First of all I fully ack with 0xA3. But if there is no way out you can wrap the dirty unmanaged dll in its own process and transfer data via IPC (TCP/IP, namedpipes, etc.). Catch all exceptions and inform the host-process. So your host-process is mostly save from memory corruption.

- 666
- 10
- 34
you can wrap the call to the unmanaged DLL with a try-catch block. AccessViolationExceptions can be caught normally. Executing the following code shows both messages:
try
{
throw new AccessViolationException();
}
catch (Exception e)
{
MessageBox.Show(e.Message + e.StackTrace, e.Message, MessageBoxButtons.OK, MessageBoxIcons.Error);
}
MessageBox.Show("Still running..");
Edit: .NET 4 introduced a change in behavior, it is no longer possible to catch corrupted state exceptions unless you specifically "ask" the runtime to do so.

- 8,539
- 4
- 63
- 74

- 6,229
- 3
- 38
- 55
-
The question is, is it a good idea to do so? It may be sensible to display a nice and friendly message to the user and write a log. But working further than that is not a good idea... – rioki Jul 22 '10 at 18:48
-
Probably not, at least not if you're not absolutely positive that nothing bad happened. – andyp Jul 22 '10 at 18:56
-
2That doesn't work. I've tried catching both Exception and AccessViolationException, but it ignores my catch block. I'm assuming that there is some app.config flag I need. – Jonathan Allen Jul 22 '10 at 19:10
-
@Sean, Normally I would say no, but this is dying in a logging routine and I'm far more willing to lose the log entry than the whole service. – Jonathan Allen Jul 22 '10 at 19:12
-
4@Jonathan Allen: Assuming from the information given by you that you are on .NET 4, the settig would be `legacyCorruptedStateExceptionsPolicy=true`. See my update. – Dirk Vollmar Jul 22 '10 at 19:31