开发者

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

I'm working with a native dll that somewhere throws a c开发者_开发问答0000005 exception (access violation) and ends up crashing my web service until the service is recycled. Is there a way to catch the exception?


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];


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

Cheers & hth.,


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜