我在我的.net应用程序中使用COM对象(MODI)。我正在调用的方法抛出一个System.AccessViolationException,它被Visual Studio拦截。奇怪的是,我已经把我的调用包装在try catch中,它具有处理AccessViolationException,COMException和其他所有事件的处理程序,但是当Visual Studio(2010)拦截AccessViolationException时,调试器会中断方法调用(doc.OCR),如果我经过,它会继续到下一行,而不是进入catch块。另外,如果我在Visual Studio之外运行,我的应用程序崩溃。我该如何处理在COM对象中引发的这个异常?如何处理AccessViolationException
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 2010-08-12 15:37:19
@ChrisF - 是的,请参阅最后的catch处理程序?这应该捕获所有内容,包括Exception和Exception的任何子类。另外,Visual Studio报告这个异常是System.AccessViolationException – Jeremy 2010-08-12 15:38:55
D'oh - 错过了,很抱歉。 – ChrisF 2010-08-12 15:47:25