首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何处理AccessViolationException

如何处理AccessViolationException
EN

Stack Overflow用户
提问于 2010-08-12 23:33:01
回答 3查看 160.5K关注 0票数 201

我在我的.net应用程序中使用COM对象(MODI)。我调用的方法抛出一个System.AccessViolationException,它被Visual Studio截获。奇怪的是,我将我的调用包装在一个try catch中,它有用于AccessViolationExceptionCOMException和其他所有东西的处理程序,但是当Visual Studio (2010)截获AccessViolationException时,调试器在方法调用(doc.OCR)上中断,如果我单步执行,它将继续到下一行,而不是进入catch块。另外,如果我在visual studio之外运行它,我的应用程序就会崩溃。如何处理在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 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();

}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3469368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档