• ASP.NET中异常处理


    Page_Error事件 > ErrorPage属性 > Application_Error事件 >  <customErrors>配置项

    第一种:页面级

       public void Page_Error(object sernder, EventArgs e)
            {
                //这里就是 我们的页面级别的遗产处理程序的呀;
                string errorMsg = String.Empty;
    
                Exception currentError = Server.GetLastError();
                errorMsg += "系统发生错误:<br/>";
                errorMsg += "错误发生的地址:" + Request.Url + "<br/>";
                errorMsg += "错误信息:"+currentError.Message;
                Response.write(errorMsg);
    
                Server.ClearError(); //清除异常否则会引发全局的 application_Error 事件
    
            }

    第二种 应用程序级

     public void Application_Error(object sender, EventArgs e)
            {
                Exception ex = Server.GetLastError();
                Exception innerEx = ex.InnerException;
                string erroMsg = string.Empty;
                string particular = string.Empty;
    
                if (innerEx != null)
                {
                    erroMsg = innerEx.Message;
                    particular = innerEx.StackTrace;//获取堆栈上的直接字符串表达式
                }
                else
                {
                    erroMsg = ex.Message;
                    particular = ex.StackTrace;
                    //异常额
                }
    
            }

    第三种:配置文件级

    模式 on off remoteonly

    On:开启自定义错误处理。

    Off:关闭自定义错误处理,当发生异常时,就会看到ASP.NET的黄页信息。

    RemoteOnly:如果在服务器上运行程序(http://localhost),当发生异常时,不会看到自定义异常信息,如果通过其他机器访问该程序,会看到自定义异常信息。该选项常用于开发人员调试程序,如果出现异常,开发人员可以通过本地访问来查看异常详细信息,而远程访问的用户看到的是自定义的异常。

    defaultRedirect 出错的时候默认重定向的url

         <system.web>  
            <customErrors mode="on" defaultRedirect="ErrorPage.html">
                 <error statuCode="403" redirect="NoAccess.html"/>
                 <error statuCode="404" redirect="FileNoFound.html"/>
             </customErrors>
         <system.web>

     第四种:

                 //最普通异常处理
                //这种事局部的异常捕获
                try
                {
    
                }
                catch (DivideByZeroException de)
                {
    
                }
                catch (ArithmeticException ae)
                {
    
                }
                catch (Exception e)
                {
                    //捕获 并处理异常;当出现多个异常并且异常之间有继承关系;
                    //捕获的顺序 子类在前,基类在后;
    
                }
                finally
                {
                    //无论什么情况下,即使catch 中有return 下,都会执行该代码块的;
                    //另外需要说明的是;finally 块中 使用break continue return 退出都是非法的;
    
    
                }

    mvc中配置文件的注意项

    http://blog.csdn.net/xxj_jing/article/details/8444640

  • 相关阅读:
    上下文管理器
    创建项目与介绍(2)
    虚拟环境的安装(1)
    爬虫-selenium(14-2)扩展
    爬虫10-1(协程)
    Python3笔记038
    Python3笔记037
    Python3笔记036
    Python3笔记035
    Python3笔记034
  • 原文地址:https://www.cnblogs.com/mc67/p/4812246.html
Copyright © 2020-2023  润新知