为了账号安全,请及时绑定邮箱和手机立即绑定

如何处理AccessViolationException

如何处理AccessViolationException

呼啦一阵风 2019-07-05 12:56:34
如何处理AccessViolationException我在.NET应用程序中使用COM对象(Modi)。我所调用的方法抛出一个System.AccessViolationException,该异常被VisualStudio截获。奇怪的是,我已经将调用包装在一个try Catch中,其中包含AccessViolationException、COMException和其他所有处理程序,但是当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();}
查看完整描述

3 回答

?
米脂

TA贡献1836条经验 获得超3个赞

在配置文件中添加以下内容,它将被捕获在TRINCATCH块中。警告的话.。尽量避免这种情况,因为这意味着某种违法行为正在发生。

<configuration>
   <runtime>
      <legacyCorruptedStateExceptionsPolicy enabled="true" />
   </runtime></configuration>


查看完整回答
反对 回复 2019-07-05
  • 3 回答
  • 0 关注
  • 975 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信