Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 673
Marty Trenouth Avatar asked Sep 14 '25 00:09

Marty Trenouth


2 Answers

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];
like image 195
Jerry Avatar answered Sep 16 '25 15:09

Jerry


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

Cheers & hth.,

like image 36
Cheers and hth. - Alf Avatar answered Sep 16 '25 14:09

Cheers and hth. - Alf