本文介绍了为什么AccessViolationException不能.NET4.0被抓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

这是非常有趣的是,下面的C#code会崩溃在.NET4.0但.NET2.0做工精细。

It is really interesting that the following C# code will crash on .NET4.0 but work fine on .NET2.0.

C#code

class Program
{
    static void Main(string[] args)
    {
        try
        {
            ExceptionTest();
            Console.WriteLine("Done!");
        }
        catch (Exception e)
        {
            Console.WriteLine("Error !!!");
            Console.WriteLine(e.Message);
        }
    }

    [DllImport("badapp")]
    private static extern int ExceptionTest();
}

C ++ code

extern "C" __declspec(dllexport) int ExceptionTest()
{
    IUnknown* pUnk = NULL;
    pUnk->AddRef();
    return 0;
}

如果编译针对.NET2.0上面的C#code,一切工作正常。只有编译它针对.NET4.0会使其崩溃在运行时。

If compiling the above C# code against .NET2.0, everything works fine. Only compiling it against .NET4.0 will make it crash at runtime.

我怀疑,因为.NET4.0的系统异常捕获机制已被更改。任何想法?

I'm suspecting that system exception catch mechanism has been changed since .NET4.0. Any ideas?

推荐答案

是的,它在.NET 4改变你抓不到指示损坏状态的异常。这是因为有pretty太多无法保证时损坏状态异常被抛出,你可以做任何事情。几乎没有任何理由要与损坏状态的过程继续执行。

Yes, it changed in .NET 4. You cannot catch exceptions that indicate a corrupted state. This is because there's pretty much no guarantee that you can do anything at all when a corrupted state exception is thrown. There is practically no reason to want a process with corrupted state to continue executing.

为了与旧code兼容性,您可以通过添加更改此行为的<$c$c>legacyCorruptedStateExceptionsPolicy元素的app.config。

For compatibility with older code, you can change this behaviour by adding the legacyCorruptedStateExceptionsPolicy element to app.config.

您也可以做到这一点根据具体情况逐案通过标记要捕获这些异常与HandleProcessCorruptedStateExceptions属性。

You can also do it on a case-by-case basis by marking methods where you want to catch these exceptions with the HandleProcessCorruptedStateExceptions attribute.

这篇关于为什么AccessViolationException不能.NET4.0被抓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 06:21