Can't catch native exception in managed code

拟墨画扇 提交于 2019-12-05 13:33:03

问题


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?


回答1:


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.




回答2:


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

try
{

}
catch
{

}


来源:https://stackoverflow.com/questions/10517199/cant-catch-native-exception-in-managed-code

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