代码改变世界

巧用Response.Write(message)中止异常

2010-11-24 20:04  dreamhappy  阅读(1031)  评论(0编辑  收藏  举报
public void ResponseInfo(string message)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(message);
            HttpContext.Current.Response.End();
        }

通常网页开发中的输出信息作为一个方法 ResponseInfo() 单独调用

但是以上方法放在 try catch中会提示 错误 System.Threading.ThreadAbortException: 正在中止线程。为什么会报这样的异常,我还没有搞明白,为了在页面上抓错我们可以换一种方式解决问题

 string error = string.Empty;   用于接收错误信息,判断是否有错误产生
 bool sucess = false;

try
            {
                     . .. ..                
               sucess = AddInfo();
            }
            catch (Exception ex)
            {
                Eagle.Common.Utils.WriteErrorToTXT(ex.ToString());
                error = "添加信息失败";
            }
            finally
            {
                if (error.Length > 0)
                    ResponseInfo(error);
            }
            if (sucess)
                ResponseInfo("sucess");
            else
            {
                ResponseInfo("faile");
            }