用心计较般般错 安心自守事事宽

cgl 坚持、努力终有所获
  新随笔  :: 管理
 在进行arcgis的GP操作时,当操作栅格图像的拼接时,报错:

AccessViolationException: 尝试读取或写入受保护的内存

 原以为可以通过try catch屏蔽掉错误,不至于程序崩溃,但是,catch不起作用。不知道为什么?后来google才发现。问题:大致意思是Net能处理托管的Structured Error Handling (SEH)异常,

但是对于非托管Corrupted State Exceptions (CSE)异常却不能捕获,如果想捕获此种类型异常,需要在配置文件中,添加一下内容

 

  <runtime>
    <legacyCorruptedStateExceptionsPolicy enabled="true" />
  </runtime>

在try catch中添加了AccessViolationException的捕获,代码如下:

  private static bool BeginRunGP(IGPProcess process, ITrackCancel TC, Action<string> pMessage)
        {
            bool result = false;
            try
            {

                if (m_geoprocessor == null)
                {
                    m_geoprocessor = new Geoprocessor();
                }

                m_geoprocessor.OverwriteOutput = true;
                //m_geoprocessor.MessagesCreated += m_geoprocessor_MessagesCreated;
                //showMessage("-------------------------------------------------------------------------------------------------------" + Environment.NewLine);
                //showMessage(string.Format("开始时间:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) + Environment.NewLine);
                if (pMessage != null)
                {
                    pMessage("GP:" + process.ToolName);
                }
                IGeoProcessorResult2 geoProcessorResult = m_geoprocessor.Execute(process, null) as IGeoProcessorResult2;
                if (geoProcessorResult != null && geoProcessorResult.Status == esriJobStatus.esriJobSucceeded)
                {
                    result = true;
                }

                if (geoProcessorResult != null)
                {
                    for (int i = 0; i < geoProcessorResult.MessageCount; i++)
                    {
                        string str = geoProcessorResult.GetMessages(i);
                        if (pMessage != null)
                        {
                            pMessage(str);
                        }
                    }
                }
                if (pMessage != null)
                {
                    pMessage("GP:" + process.ToolName + " 结束");
                }
                result = true;
            }
            catch (System.AccessViolationException ex)
            { //   AccessViolationException异常捕获
                result = false;
            }
            catch (Exception ex)     //   COMException
            {
                result = false;
                if (pMessage != null)
                {
                    pMessage("GP:" + process.ToolName + " 错误");
                    pMessage(ex.Message);
                    LogClass.PubLog.ErrorFormat("GP:{0} 错误,错误原因是:{1}", process.ToolName, ex.Message);
                }
                LogClass.PubLog.ErrorFormat("GP:{0} 错误,错误原因是:{1}", process.ToolName, ex.Message);
            }
            finally
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                for (int i = 0; i < m_geoprocessor.MessageCount; i++)
                    sb.AppendLine(m_geoprocessor.GetMessage(i));

            }
            return result;
        }

 

 

下面是他的英文解释

 
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:

(1)Recompile as a .NET 3.5 assembly and run it in .NET 4.0.

(2)Add a line to your application's config file under the configuration/runtime element:<legacyCorruptedStateExceptionsPolicy enabled="true"/>

(3)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