如何处理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个赞
<configuration> <runtime> <legacyCorruptedStateExceptionsPolicy enabled="true" /> </runtime></configuration>
- 3 回答
- 0 关注
- 1017 浏览
添加回答
举报
0/150
提交
取消