17

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?

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
DoronBM
  • 515
  • 1
  • 6
  • 17
  • Does this answer your question? [Can you catch a native exception in C# code?](https://stackoverflow.com/questions/150544/can-you-catch-a-native-exception-in-c-sharp-code) – JumpingJezza Jan 07 '22 at 03:49

2 Answers2

21

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.

M Afifi
  • 4,645
  • 2
  • 28
  • 48
-2

Catch without () will catch non-CLS compliant exceptions including native exceptions.

try
{

}
catch
{

}
Oscar
  • 13,594
  • 8
  • 47
  • 75
  • I made my comment based on this FxCop rule.. http://msdn.microsoft.com/en-gb/bb264489.aspx Sorry it didn't work.. – Oscar May 09 '12 at 20:58