谈ASP.NET全局异常处理与假窗口提示

1.异常处理思路:遇到异常就直接抛出,然后在Global.asax.cs中的Application_Error方法捕获。捕获到异常之后跳转到异常处理页面即可。
2.假窗口提示:关键还是这个假窗口要做的好看,其它的没什么。
废话不多说,先看效果图:



效果还不错吧。
实现步骤:
1.新建项目TryCatch,添加两个页面WebTest.aspx和Error.aspx。添加类、css、等,结构如下:

2.添加PageBase.cs页面,做为所有页面的基类,代码如下:
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Resources;
  8. using System.IO;
  9. using System.Reflection;
  10. using System.Data.SqlClient;
  11. namespace TryCatch
  12. {
  13.     
  14.     /// <summary>
  15.     /// the base class for Page。
  16.     /// </summary>
  17.     public class PageBase : System.Web.UI.Page
  18.     {
  19.         #region 本类构造函数
  20.         public PageBase()
  21.         {
  22.             //只有有页面继承这个类,就会为这个类的子页面自动加上这个方法
  23.             RegisterAdminPageClientScriptBlock();
  24.         }
  25.         #endregion
  26.         #region 操作提示
  27.         public void RegisterAdminPageClientScriptBlock()
  28.         {
  29.             string script = "<script language=/"javascript/"> /r/n" +
  30.                 "var spath=''/r/n" +
  31.                 "var bar=0/r/n" +
  32.                 /*"function count() /r/n" +
  33.                 "{  /r/n" +
  34.                 "   bar=bar+1; /r/n" +
  35.                 "   if (bar<100) {  /r/n" +
  36.                 "       if ( document.getElementById('success').style.display !=/"none/")  /r/n" +
  37.                 "           setTimeout(/"count()/",2000);  /r/n" +
  38.                 "   }  /r/n" +
  39.                 "   else { document.getElementById('success').style.display =/"none/"; }  /r/n" +
  40.                 "} /r/n" +*/
  41.                 "function run(spath) /r/n" +
  42.                 "{ /r/n" +
  43.                 "   if (typeof(Page_ClientValidate) == 'function') /r/n" +
  44.                 "   { /r/n" +
  45.                 "       if(Page_ClientValidate()) /r/n" +
  46.                 "       { /r/n" +
  47.                 "           document.getElementById('Layer5').innerHTML='<BR /><table><tr><td valign=top><img border=/"0/" src='+spath+'  /></td><td valign=middle style=/"font-size: 14px;/" >正在执行操作,请稍候...<BR /></td></tr></table><BR />';/r/n" +
  48.                 "           document.getElementById('success').style.display = /"block/"; /r/n" +
  49.                 //"         setInterval('count()',20000); /r/n" +
  50.                 "       } /r/n" +
  51.                 "   } /r/n" +
  52.                 "   else /r/n" +
  53.                 "   { /r/n" +
  54.                 "       document.getElementById('Layer5').innerHTML='<BR /><table><tr><td valign=top><img border=/"0/" src='+spath+'  /></td><td valign=middle style=/"font-size: 14px;/" >正在执行操作,请稍候...<BR /></td></tr></table><BR />';/r/n" +
  55.                 "       document.getElementById('success').style.display = /"block/"; /r/n" +
  56.                 //"     setInterval('count()',20000); /r/n" +
  57.                 "   } /r/n" +
  58.                 "} /r/n" +
  59.                 "</script>/r/n"+
  60.                 "<div id=/"success/" style=/"position:absolute;z-index:300;height:120px;width:284px;left:50%;top:50%;margin-left:-150px;margin-top:-80px;/">/r/n" +
  61.                 "   <div id=/"Layer2/" style=/"position:absolute;z-index:300;width:270px;height:90px;background-color: #FFFFFF;border:solid #000000 1px;font-size:14px;/">/r/n" +
  62.                 //"     <div id=/"Layer4/" style=/"height:26px;background:"+style+"line-height:26px;padding:0px 3px 0px 3px;font-weight:bolder;/">"+RM.GetString("OpTitle")+"</div>/r/n" +
  63.                 "       <table border=0 cellpadding=0 cellspacing=0 width=270px><tr style=/"height:24px;background:#9FC4FB;line-height:24px;padding:0px 3px 0px 3px;font-weight:bolder;font-size:14px;/"><td style=/"font-size:14px;/">提示</td> <td align=/"right/"><font style=/"cursor:pointer/" color=/"#ffffff/" face=/"webdings/" οnclick=/"document.getElementById('success').style.display ='none';/">r</font></td> </tr></table>/r/n" +
  64.                 "       <div id=/"Layer5/" style=/"height:64px;line-height:150%;padding:0px 3px 0px 3px;/" align=/"center/"><BR /><table><tr><td valign=top><img border=/"0/" src=/"../images/ajax_loading.gif/"  /></td><td valign=middle style=/"font-size: 14px;/" >正在执行操作,请稍候...<BR /></td></tr></table><BR /></div>/r/n" +
  65.                 "   </div>/r/n" +
  66.                 "   <div id=/"Layer3/" style=/"position:absolute;width:270px;height:90px;z-index:299;left:4px;top:5px;background-color: #E8E8E8;/"></div>/r/n" +
  67.                 "</div>/r/n" +
  68.                 "<script> /r/n" +
  69.                 "document.getElementById('success').style.display = /"none/"; /r/n" +
  70.                 "</script> /r/n" ;
  71.             //"<script language=/"JavaScript1.2/" src=/"../js/divcover.js/"></script>/r/n";
  72.             base.RegisterClientScriptBlock("Page", script);
  73.         }
  74.         #endregion
  75.     }
  76. }
3.WebTest.aspx.cs添加事件以及代码,这里要注意,这个页面是继承自 PageBase的,这样这个页面里才有这个提示div
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.SessionState;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.HtmlControls;
  11. namespace TryCatch
  12. {
  13.     /// <summary>
  14.     /// WebTest 的摘要说明。
  15.     /// </summary>
  16.     public class WebTest : PageBase
  17.     {
  18.         protected System.Web.UI.WebControls.Button Button2;
  19.         protected System.Web.UI.WebControls.Button Button1;
  20.     
  21.         private void Page_Load(object sender, System.EventArgs e)
  22.         {
  23.             if (!IsPostBack)
  24.             {
  25.                 string strpath="images/ajax_loading.gif";
  26.                 Button1.Attributes.Add("onmouseup","run('"+strpath+"');");
  27.                 Button2.Attributes.Add("OnBlur","document.getElementById('success').style.display ='none';");
  28.             }
  29.         }
  30.         private void Button1_Click(object sender, System.EventArgs e)
  31.         {
  32.             try
  33.             {
  34.                 //为了能看到提示效果,这里终止当前线程2秒
  35.                 System.Threading.Thread.Sleep(2000);
  36.                 for (int i=2;i>-1;i--)
  37.                 {
  38.                     //会报一个不能除0的异常
  39.                     int j = 10/i;
  40.                 }
  41.             }
  42.             catch (System.Exception ex)
  43.             {
  44.                 throw new Exception(ex.Message);
  45.             }
  46.         }
  47.         private void Button2_Click(object sender, System.EventArgs e)
  48.         {
  49.             throw new Exception("我是异常信息");
  50.         }
  51.         #region Web 窗体设计器生成的代码
  52.         override protected void OnInit(EventArgs e)
  53.         {
  54.             //
  55.             // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
  56.             //
  57.             InitializeComponent();
  58.             base.OnInit(e);
  59.         }
  60.         
  61.         /// <summary>
  62.         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  63.         /// 此方法的内容。
  64.         /// </summary>
  65.         private void InitializeComponent()
  66.         {    
  67.             this.Button1.Click += new System.EventHandler(this.Button1_Click);
  68.             this.Button2.Click += new System.EventHandler(this.Button2_Click);
  69.             this.Load += new System.EventHandler(this.Page_Load);
  70.         }
  71.         #endregion
  72.     }
  73. }

4.Global.asax.cs添加代码,Global.asax的作用不多说了,不知道的去搜索一下。
  1. protected void Application_Start(Object sender, EventArgs e)
  2.         {
  3.             //先初始化一下
  4.             Application["msg"]=1;
  5.             Application["tag"]=1;
  6.         }
  7. protected void Application_Error(Object sender, EventArgs e)
  8.         {
  9.             try
  10.             {
  11.                 //开始异常捕获了,这个判断是防止死锁的,如果不加会循环调用
  12.                 if(Convert.ToInt32(Application["tag"])>2)
  13.                 {
  14.                     Application["tag"]=1;
  15.                     return;
  16.                 }
  17.                 Application["tag"]=Convert.ToInt32(Application["tag"])+1;
  18.                 Exception objErr = Server.GetLastError().GetBaseException();
  19.                 Application["errorPage"] = Request.Url.ToString();
  20.                 Application["errorMsg"] =objErr.Message;
  21.                 Server.ClearError();
  22.                 Response.Redirect("Error.aspx");
  23.             }
  24.             catch{}
  25.         }
5.Error.aspx.cs代码:
  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.SessionState;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.HtmlControls;
  11. namespace TryCatch
  12. {
  13.     /// <summary>
  14.     /// Error 的摘要说明。
  15.     /// </summary>
  16.     public class Error : System.Web.UI.Page
  17.     {
  18.         protected System.Web.UI.WebControls.Label lblError;
  19.     
  20.         private void Page_Load(object sender, System.EventArgs e)
  21.         {
  22.             if(!IsPostBack)
  23.             {
  24.                 try
  25.                 {
  26.                     Application["tag"]=1;
  27.                     lblError.Text="发生异常页:"+Application["errorPage"].ToString()+"<br>异常信息:"+Application["errorMsg"].ToString();
  28.                     MessageBox.ShowPopupWin(this,"","",50000);
  29.                     WriterLog log = new WriterLog( Server.MapPath(@"./Log/Error.log"));
  30.                     log.WriterError(Application["errorPage"].ToString(),Application["errorMsg"].ToString());
  31.                 }
  32.                 catch{}
  33.             }
  34.         }
  35.         #region Web 窗体设计器生成的代码
  36.         override protected void OnInit(EventArgs e)
  37.         {
  38.             //
  39.             // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
  40.             //
  41.             InitializeComponent();
  42.             base.OnInit(e);
  43.         }
  44.         
  45.         /// <summary>
  46.         /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  47.         /// 此方法的内容。
  48.         /// </summary>
  49.         private void InitializeComponent()
  50.         {    
  51.             this.Load += new System.EventHandler(this.Page_Load);
  52.         }
  53.         #endregion
  54.     }
  55. }
6.要注意一下Error.log是记录错误日志的,要有写的权限,不然是写不进去的。

上面只是思路,我把Demo放这里给大家下载:

新浪下载    CSDN下载
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

满衣兄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值