1

I am puzzled. An AccessViolationException is randomly raised by a third-party library. It can safely be ignored, so I am wrapping the calling method in a [HandleProcessCorruptedStateExceptions] attribute as suggested here.

However, I am still seeing the exception getting raised as visible below: enter image description here

I am using .NET Framework 4.6.2 and Visual Studio 2015 Update 3. What could I have missed?

Community
  • 1
  • 1
Erwin Mayer
  • 18,076
  • 9
  • 88
  • 126
  • Shouldn't [HandleProcessCorruptedStateExceptions] work whatever the object being passed? How would it matter? – Erwin Mayer Oct 18 '16 at 14:37
  • 1
    Erm, wait, it worked. The exception did not terminate your program as a CSE normally does. You promised you would handle the exception with the attribute but you didn't. Be careful to not abuse the attribute, you cannot call that 3rd party library again. That requires you to either terminate your program with a decent error report (subscribe the AppDomain.UnhandledException event) or set a static bool variable that suppresses any further calls to that library. Keep in mind that the debugger always steps in before the event is raised. – Hans Passant Oct 18 '16 at 15:35
  • @HansPassant You are absolutely correct! (In fact, this is without exaggeration almost always the case because you are, in my opinion, one of the smartest people here.) After an `AccessViolationException` nothing is really safe anymore except terminating the process. I didn't mention your precautionary warnings in my answer because the OP stated that it would be safe to ignore this exception and I just wanted to respect that. (I assumed that he had already analyzed the reasons for the exception.) – haindl Oct 19 '16 at 15:37

1 Answers1

1

You forgot to insert a try/catch around table.Start().

[HandleProcessCorruptedStateExceptions] definitely needs a try/catch to catch that AccessViolationException.

So your code should be:

[HandleProcessCorruptedStateExceptions]
private static void StartTable(Table table) {
    try
    {
        table.Start();
    }
    catch (AccessViolationException)
    {
        // Ignore
    }
}

You can take a look at here or here for references.

Community
  • 1
  • 1
haindl
  • 3,111
  • 2
  • 25
  • 31
  • Thanks! I had included the `try/catch` block but in the method calling `StartTable`, which most likely was not enough. – Erwin Mayer Oct 19 '16 at 02:19
  • 1
    @ErwinMayer You're welcome! :-) Yes, the `try/catch` needs to be directly in the method that has the `[HandleProcessCorruptedStateExceptions]` set. (Delegates of any kind are also separate methods from the CLR perspective.) – haindl Oct 19 '16 at 08:08