0

A third party library is causing a AccessViolationException everytime the library opens a certain file. I'm trying to iterate through thousands of files, so it would be useful to catch the exception, add the item to a list of files which aren't to be opened and continue. However, the Exception isn't caught by the catch(Exception e) block, nor by the specific catch(AccessViolationException ave) block.

In a comment to another question, John Saunders suggests that an AccessViolationException can be caught, but that one shouldn't do so.

What's a programmer to do?

Community
  • 1
  • 1
lowerkey
  • 8,105
  • 17
  • 68
  • 102
  • 1
    How do you know that the access violation hasn't corrupted your app's memory? You don't, so you can't just ignore it and continue. – Panagiotis Kanavos Jun 06 '12 at 14:09
  • 2
    Have you tried using the [AppDomain.UnhandledExection](http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx) to catch anything in the appdomain that is not handled - this will give you an opportunity to interogate the exception. You can remove this when you are confident you know what is going on. – RobertMS Jun 06 '12 at 14:12

1 Answers1

3

You probably don't want to catch an AccessViolationException for the sole reason you can not guarantee the exception hasn't corrupted the state of your application.

By default, because of the reason above, execution won't reach the catch block. If you explicitly want to handle process corrupting exceptions, you need to decorate your class with theHandleProcessCorruptedStateExceptions attribute.

aevitas
  • 3,753
  • 2
  • 28
  • 39