我在我的.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