无法在托管代码中捕获本机异常 [英] Can't catch native exception in managed code

查看:117
本文介绍了无法在托管代码中捕获本机异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个混合的.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:


  1. 尝试/捕获块

  2. AppDomain.UnHandledExption + = ...

  3. RuntimeCompatibilityAttribute(WrapNonExceptionThrows = true)

  1. Try/catch block
  2. AppDomain.UnHandledExption += ...
  3. 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.

更多此处, http://msdn.microsoft.com/en-us/magazine/dd419661.aspx

请注意该文章中的注释,例如,如果您想正常捕获它们,而不是遵循它们执行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 legacyCorruptedState­­ExceptionsPolicy=true into your config file.

这篇关于无法在托管代码中捕获本机异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆