第二十章 排查和调试Web程序 之 设计异常处理策略

1. 概述

  本章内容包括: 多层架构中的异常处理、使用global.asax 或 自定义的HttpHandler 或 web.config中的属性来显示特定的错误页、处理 first chance 异常。

2. 主要内容

  2.1 多层架构中的异常处理

    多层架构一般是单向依赖的。

    传统的 数据-逻辑-表现 三层结构,数据层可以将自己无法处理的异常向上抛出,逻辑层捕获到异常,需要决定如何处理。而表现层只需要考虑如何展示给用户就可以了。

    ASP.NET MVC中也是同理,Model层的异常,经过Controller的处理,然后抛给UI。

  2.2 自定义错误页、自定义HttpHandler 和 设置web.config属性

    1. 在标准的MVC中,可以使用controller调用ErrorManagerController,为每种Http状态提供多种action方法来处理。

    2. Global.asax 也是一种支持自定义错误页的方式。

public void Application_Error(Object sender, EventArgs e) 
{ 
    if (Server != null) 
    { 
        //Get the context 
        HttpContext appContext = ((MvcApplication)sender).Context; 
        Exception ex = Server.GetLastError().GetBaseException(); 
        //Log the error using the logging framework 
        Logger.Error(ex);  
        //Clear the last error on the server so that custom errors are not fired 
        Server.ClearError(); 
        //forward the user to the error manager controller. 
        IController errorController = new ErrorManagerController(); 
        RouteData routeData = new RouteData(); 
        routeData.Values["controller"] = "ErrorManagerController"; 
        routeData.Values["action"] = "ServerError"; 
        errorController.Execute( 
            new RequestContext(new HttpContextWrapper(appContext), routeData)); 
    } 
}

    3. 可以在web.config中配置错误页。

<customErrors mode="RemoteOnly" defaultRedirect="ErrorManager/ServerError"> 
  <error statusCode="400" redirect="ErrorManager/Status400" /> 
  <error statusCode="403" redirect="ErrorManager/Status403" /> 
  <error statusCode="404" redirect="ErrorManager/Status404" /> 
</customErrors>

  2.3 处理第一轮异常(first chance exceptions)

    第一轮异常是 还未被任何异常处理程序处理过的异常。

    在调试模式捕获第一轮异常可以有效的避免异常进入生产环境中。

    还可以通过在Global.asax中插入代码来捕获隐藏的第一轮异常。

protected void Application_Start() 
{ 
    AppDomain.CurrentDomain.FirstChanceException +=  
              CurrentDomain_FirstChanceException; 
 
    AreaRegistration.RegisterAllAreas(); 
    WebApiConfig.Register(GlobalConfiguration.Configuration); 
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
    RouteConfig.RegisterRoutes(RouteTable.Routes); 
    BundleConfig.RegisterBundles(BundleTable.Bundles); 
    AuthConfig.RegisterAuth(); 
} 
 
protected void CurrentDomain_FirstChanceException(object sender,  
         System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e) 
{ 
    if (e.Exception is NotImplementedException) 
    { 
        // do something special when the functionality is not implemented 
    } 
}

3. 总结

  ① 异常可能发生在程序的任何地方,不同的地方,处理方式可能是不一样。多层程序中,一般需要处理本层的和来自下一层的异常。

  ② 可以在程序中创建自定义的错误页。MVC中,可以用Controller来处理异常,然后生成各种view来展示各种异常信息。

  ③ 第一轮异常是 还未被任何异常处理程序处理过的异常。可以处理中添加日志、检测或者清理操作,但是要确保递归处理造成堆栈溢出。

posted @ 2016-05-12 15:34  stone lv  阅读(214)  评论(0编辑  收藏  举报