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

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

问题描述

我正在使用.net应用程序中的COM对象(MODI)。我调用的方法会抛出一个System.AccessViolationException,它被Visual Studio拦截。奇怪的是,我已经把我的调用包装在一个try catch中,它具有AccessViolationException,COMException和其他一切的处理程序,但是当Visual Studio(2010)拦截AccessViolationException时,调试器会在方法调用(doc.OCR)中断,如果我通过,它继续下一行,而不是进入catch块。此外,如果我在视觉工作室外运行,我的应用程序崩溃。如何处理COM对象中抛出的异常?

  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似乎因为某些原因而获得访问冲突,但仍然可以返回OCR文本。
sText = doc.Images [0] .Layout.Text;
}
catch(System.Runtime.InteropServices.COMException ex)
{
//如果没有文本,引擎会引发异常。
sText =;
}
catch
{
sText =;
}

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

//清理例程,这是我们如何删除MODI使用的文件。
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc);
doc = null;
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

}


解决方案

NET 4.0,运行时处理作为Windows结构化错误处理(SEH)错误引发的某些异常作为损坏状态的指示符。这些损坏的状态异常(CSE)不允许被您的标准托管代码所捕获。我不会进入为什么或如何在这里。阅读这篇关于CSE在.NET 4.0 Framework中的文章:



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



但有希望。有几种方法可以解决这个问题:


  1. 重新编译为.NET 3.5程序集,并在.NET 4.0中运行。


  2. 在配置/运行时元素下,向应用程序的配置文件添加一行:
    < legacyCorruptedStateExceptionsPolicy enabled =true | false/>


  3. 使用 HandleProcessCorruptedStateExceptions装饰想要捕获这些异常的方法属性。请参阅 http://msdn.microsoft.com/en-us/magazine/ dd419661.aspx#id0070035 的详细信息。


有关更多参考资料:
http://connect.microsoft.com/VisualStudio/feedback/details/ 557105 /无法抓取访问权限异常


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天全站免登陆