Chinaunix首页 | 论坛 | 博客
  • 博客访问: 80457
  • 博文数量: 64
  • 博客积分: 1545
  • 博客等级: 上尉
  • 技术积分: 392
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-23 15:23
文章分类

全部博文(64)

文章存档

2013年(1)

2012年(63)

我的朋友

分类: 嵌入式

2012-11-14 15:00:50

要创建一个错误处理的代码ErrHandler.cs

点击(此处)折叠或打开

  1.     /// Handles error by accepting the error message
  2.   /// Displays the page on which the error occured
  3.   public static void WriteError(string errorMessage)
  4.   {
  5.     try
  6.     {
  7.       string path = "~/Error/" + DateTime.Today.ToString("dd-mm-yy") + ".txt";
  8.       if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
  9.       {
  10.         File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
  11.       }
  12.       using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
  13.       {
  14.         w.WriteLine("\r\nLog Entry : ");
  15.         w.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
  16.         string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() +
  17.                ". Error Message:" + errorMessage;
  18.         w.WriteLine(err);
  19.         w.WriteLine("__________________________");
  20.         w.Flush();
  21.         w.Close();
  22.       }
  23.     }
  24.     catch (Exception ex)
  25.     {
  26.       WriteError(ex.Message);
  27.     }
  28.   }


这就是我们的ErrHandler类了.然后我们来看看如何使用这个类和在Page级中(Application级中)处理错误。

在Page级中处理错误

在Default.aspx中,从工具箱中添加一个button控件.将这个button命名为 btnError 并设置值为 "Throw Handled Exception".我们将抛出一个异常.

按钮点击操作代码如下:

点击(此处)折叠或打开

  1. protected void btnHandled_Click(object sender, EventArgs e)
  2.   {
  3.     try
  4.     {
  5.       throw new Exception("Sample Exception");
  6.     }
  7.     catch (Exception ex)
  8.     {
  9.       // Log the error to a text file in the Error folder
  10.       ErrHandler.WriteError(ex.Message);
  11.     }
  12. }

注意:要在根目录建立Error文件夹

Redirecting users on unhandled errors(在未有处理错误情况下重定向用户)

如何在Application级上来捕捉未有错误处理而发生的错误,并将用户定向到一个不同的页面。

根目录添加一个 Global.asax 文件

点击(此处)折叠或打开

  1. void Application_Error(object sender, EventArgs e)
  2.   {
  3.     // Code that runs when an unhandled error occurs
  4.     Exception objErr = Server.GetLastError().GetBaseException();
  5.     string err = "Error in: " + Request.Url.ToString() +
  6.              ". Error Message:" + objErr.Message.ToString();
  7.     // Log the error
  8.     ErrHandler.WriteError(err);    
  9.   }

打开你的 Web.config 文件,并定位到 标签处,取消注释改成下面代码:

点击(此处)折叠或打开

  1. <customErrors mode="On" defaultRedirect="ErrorPage.aspx">
  2.             <error statusCode="403" redirect="NoAccess.htm" />
  3.             <error statusCode="404" redirect="FileNotFound.htm" />
  4.  </customErrors>
新建ErrorPage.aspx文件

为了测试这个功能,我们回到 Default.aspx, 添加新的按钮并命名为 btnUnhandled 并将文本属性设置为 Throw Unhandled Exception.我们将使用"Divide By Zero"异常.并不去处理它.我们可以发现少了 catch 块.所以当错误发生时,用户就会按照我们在web.confg文件中设置的重定向到 "ErrorPage.aspx".

点击(此处)折叠或打开

  1. protected void btnHandled_Click(object sender, EventArgs e)
  2. {
  3.    int i = 9;
  4.    int j = 0;
  5.    Respone.Write( i / j );
  6. }


英文版:http://www.dotnetcurry.com/ShowArticle.aspx?ID=94&AspxAutoDetectCookieSupport=1




阅读(392) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~