问题描述
我有一个混合的.NET和本机代码控制台应用程序。由于Visual C运行时库出现致命错误,导致应用程序进程终止。即使我使用以下内容,托管代码也无法捕获本机异常:
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:
- 尝试/捕获块
-
AppDomain.UnHandledExption + = ...
- 将
RuntimeCompatibilityAttribute(WrapNonExceptionThrows = true)
。
- Try/catch block
AppDomain.UnHandledExption += ...
- Marking the
RuntimeCompatibilityAttribute(WrapNonExceptionThrows = true)
in the AssmblyInfo file.
我还能做什么?
推荐答案
.NET 4中的本机异常已更改,因此无法使用标准catch块对其进行捕获。您必须专门将引发异常的函数标记为 [HandleProcessCorruptedStateExceptions]
才能捕获它。
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.
更多此处,
请注意该文章中的注释,例如,如果您想正常捕获它们,而不是遵循它们执行finally块并退出的建议,将 legacyCorruptedStateExceptionsPolicy = true
添加到您的配置文件中。
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 legacyCorruptedStateExceptionsPolicy=true
into your config file.
这篇关于无法在托管代码中捕获本机异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!