我正在从.net应用程序中使用COM对象(MODI)。我正在调用的方法将引发System.AccessViolationException,它会被Visual Studio拦截。奇怪的是,我将调用包裹在一个try捕获中,该捕获具有用于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
处理程序放入(临时!)以捕获所有异常并查看异常实际是什么?