Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't catch native exception in managed code

I have a mixed .NET and native code console application. The application process is terminated due to Visual C RunTime Library fatal error. Even though I am using the following, the managed code doesn’t catch the native exception:

  1. Try/catch block
  2. AppDomain.UnHandledExption += ...
  3. Marking the RuntimeCompatibilityAttribute(WrapNonExceptionThrows = true) in the AssmblyInfo file.

What else can I do?

like image 677
DoronBM Avatar asked May 09 '12 13:05

DoronBM


1 Answers

Native exceptions have changed in .NET 4 so that they can not be catched with a standard catch block. You specifically have to mark the function where the exception is being thrown as [HandleProcessCorruptedStateExceptions] to be able to catch it.

More here, http://msdn.microsoft.com/en-us/magazine/dd419661.aspx

Watch out for the notes in that article, like if you'd like to catch them normally rather than follow their advice of executing the finally block and exiting, add legacyCorruptedState­­ExceptionsPolicy=true into your config file.

like image 136
M Afifi Avatar answered Nov 15 '22 22:11

M Afifi