简体   繁体   English

无法捕获托管代码中的本机异常

[英]Can't catch native exception in managed code

I have a mixed .NET and native code console application. 我有一个混合的.NET和本机代码控制台应用程序。 The application process is terminated due to Visual C RunTime Library fatal error. 由于Visual C RunTime Library致命错误,应用程序进程终止。 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. 在AssmblyInfo文件中标记RuntimeCompatibilityAttribute(WrapNonExceptionThrows = true)

What else can I do? 我还可以做些什么?

Native exceptions have changed in .NET 4 so that they can not be catched with a standard catch block. .NET 4中的本机异常已更改,因此无法使用标准catch块捕获它们。 You specifically have to mark the function where the exception is being thrown as [HandleProcessCorruptedStateExceptions] to be able to catch it. 您特别需要将抛出异常的函数标记为[HandleProcessCorruptedStateExceptions]以便能够捕获它。

More here, http://msdn.microsoft.com/en-us/magazine/dd419661.aspx 更多信息,请访问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 legacyCorruptedStateExceptionsPolicy=true into your config file. 请注意该文章中的注释,如果您想要正常捕获它们而不是遵循执行finally块和退出的建议,请将legacyCorruptedStateExceptionsPolicy=true添加到配置文件中。

Catch without () will catch non-CLS compliant exceptions including native exceptions. Catch without()将捕获非CLS兼容的异常,包括本机异常。

try
{

}
catch
{

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM