How do I catch c0000005 exception from native dll in c#

雨燕双飞 提交于 2019-12-24 00:55:11

问题


I'm working with a native dll that somewhere throws a c0000005 exception (access violation) and ends up crashing my web service until the service is recycled. Is there a way to catch the exception?


回答1:


you can catch the exception using Microsoft SEH exception handler, but really you should fix whatever is wrong.

Cheers & hth.,




回答2:


I agree with the others... Fix the problem, but sometimes you inherit code and you just want to catch an unexpected violation in production.

In .net 4+ you can add the HandleProcessCorruptedStateExceptions attribute and it will come out as an Exception that you can catch. The call stack isn't horribly useful, but it's better than nothing.

C#

[System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions]
public void SomeCSharpMethod()
try
{
  // call managed code
}
catch (Exception ex)
{
  Console.WriteLine("Exception");
}

C++

vector<int> myValues;
int a = myValues[1];



回答3:


Don't catch this. Fix the bug.

0xc0000005 is the NT error code for STATUS_ACCESS_VIOLATION. This means your program made a bad pointer dereference. Put very simply this means your program crashed violently and attempting to recover is misguided.

I know you say it's a 3rd party DLL, but at very least you should debug and understand the issue. It may be something simple like you are passing the DLL some bad input or not initializing it properly. If you can't do that you can contact the authors of the DLL or consider eliminating your dependency on them.



来源:https://stackoverflow.com/questions/7030032/how-do-i-catch-c0000005-exception-from-native-dll-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!