【发布时间】:2020-07-24 03:03:49
【问题描述】:
我在我的 .net 应用程序中使用 COM 对象 (MODI)。我调用的方法会抛出一个System.AccessViolationException,它会被 Visual Studio 拦截。奇怪的是,我已经将我的调用包装在一个 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();
}
【问题讨论】:
-
您是否尝试过(暂时!)放置一个
Exception处理程序来捕获所有异常并查看异常实际上是什么? -
@ChrisF - 是的,看到最后一个 catch 处理程序了吗?这应该捕获所有内容,包括异常和异常的任何子类。同样,Visual Studio 报告异常是 System.AccessViolationException
标签: c# .net exception com process-state-exception