• 未处理AccessViolationException 异常


     在进行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

  • 相关阅读:
    linux学习-----项目上线步骤
    linux学习-----数据库MySQL
    linux学习-----shell基础
    linux学习-----网络基础,网络相关命令,项目上线流程
    linux学习-----linux权限,sudo的使用
    linux学习-----开机启动项设置,ntp服务,防火墙服务,rpm服务,cron服务
    linux学习-----用户,用户组管理 网络设置 ssh服务
    linux学习-----vim编辑器的使用
    linux学习-----指令学习2 及练习
    linux学习-----指令学习1
  • 原文地址:https://www.cnblogs.com/cglNet/p/6913024.html
Copyright © 2020-2023  润新知