我在我的.net应用程序中使用COM对象(MODI)。我调用的方法抛出一个System.AccessViolationException
,它被Visual Studio截获。奇怪的是,我将我的调用包装在一个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();
}
发布于 2014-01-27 23:25:14
在配置文件中添加以下内容,它将被捕获到try catch块中。警告一下..。尽量避免这种情况,因为这意味着正在发生某种违规行为。
<configuration>
<runtime>
<legacyCorruptedStateExceptionsPolicy enabled="true" />
</runtime>
</configuration>
发布于 2017-03-23 02:08:50
从上面的答案编译而来,对我来说很有效,做了以下步骤来捕捉它。
第1步-将以下代码片段添加到配置文件中
<configuration>
<runtime>
<legacyCorruptedStateExceptionsPolicy enabled="true" />
</runtime>
</configuration>
第二步
添加-
[HandleProcessCorruptedStateExceptions]
[SecurityCritical]
在函数的顶部,您需要捕获异常
来源:http://www.gisremotesensing.com/2017/03/catch-exception-attempted-to-read-or.html
发布于 2010-08-13 01:06:51
https://stackoverflow.com/questions/3469368
复制相似问题