如何处理AccessViolationException [英] How to handle AccessViolationException

查看:593
本文介绍了如何处理AccessViolationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是COM对象(MODI)从我的.NET应用程序中。我打电话的方法引发System.AccessViolationException,这是由Visual Studio截获。奇怪的是,我已经包裹着我在尝试捕捉,它具有处理程序AccessViolationException,收到COMException和其他一切电话,但是当Visual Studio中(2010年)拦截AccessViolationException,在方法调用调试中断(doc.OCR)如果我步,继续到下一行,而不是进入catch块。此外,如果我跑在Visual Studio这超出了我的应用程序崩溃。我该如何处理这个异常是COM对象中抛出?

  MODI.Document DOC =新MODI.Document();
尝试
{
    doc.Create(sFileName);
    尝试
    {
        doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH,假的,假的);
        STEXT = doc.Images [0] .Layout.Text;
    }
    赶上(System.AccessViolationException前)
    {
        // MODI似乎得到访问违规出于某种原因,但仍然能够返回OCR文字。
        STEXT = doc.Images [0] .Layout.Text;
    }
    赶上(System.Runtime.InteropServices.COMException前)
    {
        //如果没有文本存在时,发动机会抛出异常。
        STEXT =;
    }
    抓住
    {
        STEXT =;
    }

    如果(STEXT!= NULL)
    {
        STEXT = sText.Trim();
    }
}
最后
{
    doc.Close(假);

    //清理程序,这就是我们如何能够删除使用MODI文件。
    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(DOC);
    DOC = NULL;
    GC.WaitForPendingFinalizers();
    所以GC.Collect();
    GC.WaitForPendingFinalizers();

}
 

解决方案

在.NET 4.0中,运行时处理提出了与Windows结构化错误处理(SEH)错误,损坏状态的指标,某些例外。这些损坏的状态异常(CSE)是不允许被开发者的标准管理code。我不会进入的原因还是怎么在这里。阅读这篇文章,关于CSE的在.NET 4.0框架:

<一个href="http://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035">http://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035

不过,有希望。有几种方法来解决这个问题:

  1. 重新编译为.NET 3.5组装和运行在.NET 4.0中。

  2. 添加一行配置/运行元素应用程序的配置文件: &LT; legacyCorruptedStateExceptionsPolicy启用=真|假/&GT;

  3. 装饰要与 HandleProcessCorruptedStateExceptions 属性捕获这些异常的方法。请参阅<一href="http://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035">http://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035了解详细信息。

有关更多参考: <一href="http://connect.microsoft.com/VisualStudio/feedback/details/557105/unable-to-catch-accessviolationexception">http://connect.microsoft.com/VisualStudio/feedback/details/557105/unable-to-catch-accessviolationexception

I am using a COM object (MODI) from within my .net application. The method I am calling throws a System.AccessViolationException, which is intercepted by Visual Studio. The odd thing is that I have wrapped my call in a try catch, which has handlers for AccessViolationException, COMException and everything else, but when Visual Studio (2010) intercepts the AccessViolationException, the debugger breaks on the method call (doc.OCR), and if I step through, it continues to the next line instead of entering the catch block. Additionally, if I run this outside of the visual studio my application crashes. How can I handle this exception that is thrown within the COM object?

MODI.Document doc = new MODI.Document();
try
{
    doc.Create(sFileName);
    try
    {
        doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false);
        sText = doc.Images[0].Layout.Text;
    }
    catch (System.AccessViolationException ex)
    {
        //MODI seems to get access violations for some reason, but is still able to return the OCR text.
        sText = doc.Images[0].Layout.Text;
    }
    catch (System.Runtime.InteropServices.COMException ex)
    {
        //if no text exists, the engine throws an exception.
        sText = "";
    }
    catch
    {
        sText = "";
    }

    if (sText != null)
    {
        sText = sText.Trim();
    }
}
finally
{
    doc.Close(false);

    //Cleanup routine, this is how we are able to delete files used by MODI.
    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc);
    doc = null;
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();

}

解决方案

In .NET 4.0, the runtime handles certain exceptions raised as Windows Structured Error Handling (SEH) errors as indicators of Corrupted State. These Corrupted State Exceptions (CSE) are not allowed to be caught by your standard managed code. I won't get into the why's or how's here. Read this article about CSE's in the .NET 4.0 Framework:

http://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035

But there is hope. There are a few ways to get around this:

  1. Recompile as a .NET 3.5 assembly and run it in .NET 4.0.

  2. Add a line to your application's config file under the configuration/runtime element: <legacyCorruptedStateExceptionsPolicy enabled="true|false"/>

  3. Decorate the methods you want to catch these exceptions in with the HandleProcessCorruptedStateExceptions attribute. See http://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035 for details.

For more reference: http://connect.microsoft.com/VisualStudio/feedback/details/557105/unable-to-catch-accessviolationexception

这篇关于如何处理AccessViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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