System.AccessViolationException unhandled by catch exception statement RRS feed

  • Question

  • I have a legacy C++ dll used as interop in C# application. I used following code block and thought it should catch all exceptions. However, I am seeing System.AccessViolationException unhandled in the line of _myLegacyDll.DoWork();

    Why can't this exception be catched?

    // some code
    // some code
    catch (System.Runtime.InteropServices.SEHException x)
    // catch logic
    catch (Exception x)
    // catch logic

    Thursday, August 23, 2012 4:36 PM


All replies

  • Do you:

    - have the execution stop at that line when you're debugging?


    - get a dialog box stating that exception is thrown at that line when running the application?

    Friday, August 24, 2012 6:37 AM
  • I also would like to know the answer to's question.

    And I would like you to share us the exception StackTrace and the InnerException if it has content in it.

    Without those info, it's hard for us to helping you.

    And if you can share us a reproducible Visual Studio Solution, and the reproducible operation steps list, it will be helpful:

    you can use the skydrive to share things:

    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    Friday, August 24, 2012 8:40 AM
  • I run the solution in debug mode from visual studio. I don't have any debug point specific. However, I got the dialog box stating that exception is thrown at that line when running the application from VS.

    Mike - The code is exact same as what I posted. It is not 100% reproducable. It happens several times for a while. I can't post the C++ Dlls and code to any place because of ..

    Friday, August 24, 2012 1:42 PM
  • If it happens in debug, it can be because VS is setup to stop at the point an exception is thrown. You can check, or uncheck, specific exception types, or whole categories of exception types in menu Debug\Exceptions...
    Friday, August 24, 2012 4:10 PM
  • It happens in both debug/release mode. I have built and deploy binaries to server running as windows service. After run a while, I can see the service crashed and logged following exceptions in EventView. Why the try-catch doesn't work?

    Application: <>
    Framework Version: v4.0.30319
    Description: The process was terminated due to an unhandled exception.
    Exception Info: System.AccessViolationException

    Thursday, August 30, 2012 6:40 PM
  • Such exception cannot be caught in .NET 4.0 by default. If you really need to do this (usually it's a bad idea) then you can apply the following attribute to the method that contains the try/catch:

    Thursday, August 30, 2012 6:50 PM
  • Hi I am getting exactly the same exception and crashing the server.

    Did you get the solution for this ??

    Plz reply if u hv found solution.

    Thursday, September 12, 2013 4:57 PM
  • One solution is already mentioned my previous post. Another one is to set legacyCorruptedStateExceptionsPolicy to enabled in the configuration file:

    Both solutions should be applied with care because AccessViolationExceptions can indicate a serious error that may lead to data corruption and other problems.

    Thursday, September 12, 2013 7:03 PM