如何處理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 關注
- 1045 瀏覽
添加回答
舉報
0/150
提交
取消