如何處理AccessViolationException我在.NET應(yīng)用程序中使用COM對(duì)象(Modi)。我所調(diào)用的方法拋出一個(gè)System.AccessViolationException,該異常被VisualStudio截獲。奇怪的是,我已經(jīng)將調(diào)用包裝在一個(gè)try Catch中,其中包含AccessViolationException、COMException和其他所有處理程序,但是當(dāng)Visual Studio(2010)攔截AccessViolationException時(shí),調(diào)試器就會(huì)中斷方法調(diào)用(doc.OCR),如果我逐步執(zhí)行,它將繼續(xù)到下一行,而不是進(jìn)入CATCH塊。此外,如果我在visual studio之外運(yùn)行這個(gè)程序,我的應(yīng)用程序就會(huì)崩潰。如何處理COM對(duì)象中引發(fā)的異常?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();}
如何處理AccessViolationException
呼啦一陣風(fēng)
2019-07-05 12:56:34