如何处理AccessViolationException [英] How to handle 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似乎因为某些原因而获得访问冲突,但仍然可以返回OCR文本。
sText = doc.Images [0] .Layout.Text;
}
catch(System.Runtime.InteropServices.COMException ex)
{
//如果没有文本,引擎会引发异常。
sText =;
}
catch
{
sText =;
}
if(sText!= null)
{
sText = sText.Trim();
}
}
finally
{
doc.Close(false);
//清理例程,这是我们如何删除MODI使用的文件。
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doc);
doc = null;
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
NET 4.0,运行时处理作为Windows结构化错误处理(SEH)错误引发的某些异常作为损坏状态的指示符。这些损坏的状态异常(CSE)不允许被您的标准托管代码所捕获。我不会进入为什么或如何在这里。阅读这篇关于CSE在.NET 4.0 Framework中的文章:
http://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035
但有希望。有几种方法可以解决这个问题:
-
重新编译为.NET 3.5程序集,并在.NET 4.0中运行。
-
在配置/运行时元素下,向应用程序的配置文件添加一行:
< legacyCorruptedStateExceptionsPolicy enabled =true | false/>
-
使用
HandleProcessCorruptedStateExceptions装饰想要捕获这些异常的方法
属性。请参阅 http://msdn.microsoft.com/en-us/magazine/ dd419661.aspx#id0070035 的详细信息。
有关更多参考资料:
http://connect.microsoft.com/VisualStudio/feedback/details/ 557105 /无法抓取访问权限异常
I am using a COM object (MODI) from within my .net application. The method I am calling throws a System.AccessViolationException, which is intercepted by Visual Studio. The odd thing is that I have wrapped my call in a try catch, which has handlers for AccessViolationException, COMException and everything else, but when Visual Studio (2010) intercepts the AccessViolationException, the debugger breaks on the method call (doc.OCR), and if I step through, it continues to the next line instead of entering the catch block. Additionally, if I run this outside of the visual studio my application crashes. How can I handle this exception that is thrown within the COM object?
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();
}
In .NET 4.0, the runtime handles certain exceptions raised as Windows Structured Error Handling (SEH) errors as indicators of Corrupted State. These Corrupted State Exceptions (CSE) are not allowed to be caught by your standard managed code. I won't get into the why's or how's here. Read this article about CSE's in the .NET 4.0 Framework:
http://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035
But there is hope. There are a few ways to get around this:
Recompile as a .NET 3.5 assembly and run it in .NET 4.0.
Add a line to your application's config file under the configuration/runtime element:
<legacyCorruptedStateExceptionsPolicy enabled="true|false"/>
Decorate the methods you want to catch these exceptions in with the
HandleProcessCorruptedStateExceptions
attribute. See http://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035 for details.
For more reference: http://connect.microsoft.com/VisualStudio/feedback/details/557105/unable-to-catch-accessviolationexception
这篇关于如何处理AccessViolationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!