異常處理
在 ASP.NET 中異常處理有三個方面:
- Tracing – 在頁面級或者應用程序級追蹤程序執行。
- Error handling – 在頁面級或者應用程序級解決標準錯誤或者自定義錯誤。
- Debugging – 在程序中前進,設置斷點來分析代碼。
在這一章中,我們將討論 tracing 和 handling。并且在這一章中,我們將涉及 debugging。
為了理解概念,創建以下的樣本應用程序。它有一個 label 控件,一個 dropdown 列表和一個鏈接。dropdown 列表加載了一個名言的 array 列表并且被選擇的引用將顯示在下面的標簽中。它也擁有一個超鏈接,它指向一個不存在的鏈接。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="errorhandling._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.ythuaji.com.cn/uploads/allimg/3laceybn4sd" > <head runat="server"> <title> Tracing, debugging and error handling </title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lblheading" runat="server" Text="Tracing, Debuggin and Error Handling"> </asp:Label> <br /> <br /> <asp:DropDownList ID="ddlquotes" runat="server" AutoPostBack="True" onselectedindexchanged="ddlquotes_SelectedIndexChanged"> </asp:DropDownList> <br /> <br /> <asp:Label ID="lblquotes" runat="server"> </asp:Label> <br /> <br /> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="mylink.htm">Link to:</asp:HyperLink> </div> </form> </body> </html>文件后的代碼:
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string[,] quotes = { {"Imagination is more important than Knowledge.", "Albert Einsten"}, {"Assume a virtue, if you have it not" "Shakespeare"}, {"A man cannot be comfortable without his own approval", "Mark Twain"}, {"Beware the young doctor and the old barber", "Benjamin Franklin"}, {"Whatever begun in anger ends in shame", "Benjamin Franklin"} }; for (int i=0; i<quotes.GetLength(0); i++) ddlquotes.Items.Add(new ListItem(quotes[i,0], quotes[i,1])); } } protected void ddlquotes_SelectedIndexChanged(object sender, EventArgs e) { if (ddlquotes.SelectedIndex != -1) { lblquotes.Text = String.Format("{0}, Quote: {1}", ddlquotes.SelectedItem.Text, ddlquotes.SelectedValue); } } }Tracing
為了允許頁面級別的追蹤,你需要修改 Page 指令并且如下添加一個 Trace 屬性:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="errorhandling._Default" Trace ="true" %>現在當你執行文件時,你將得到追蹤信息:
運行來觀察影響:
Try-Catch 是一個 C# 編程結構。try 塊持有任何可以或不可以產生錯誤的代碼,catch 塊捕獲了錯誤。當程序運行時,它在 trace 日志中發送警告。
錯誤解決
盡管 ASP.NET 能檢測所有的運行時錯誤,仍然有一些微小的錯誤仍在那兒。通過追蹤觀察錯誤是為開發者準備的,而不是用戶。
因此,為了攔截這樣情況的發生,你可以在應用程序的 web.config 中添加錯誤解決設置。它是應用程序范圍的錯誤解決。例如,你可以在 web.config 文件中添加以下的代碼:
<configuration> <system.web> <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm" /> <error statusCode="404" redirect="FileNotFound.htm" /> </customErrors> </system.web> <configuration>部分有可能的屬性: – **Mode:**它允許或者不允許自定義錯誤頁面。它有三個可能的值: – **On:**展示自定義頁面。 – **Off:**展示 ASP.NET 錯誤頁面(黃色頁面) – **remoteOnly:**它展示了自定義錯誤到客戶端,展示本地的 ASP.NET 錯誤。- **defaultRedirect:**它含有頁面的 URL 來展示以備不能解決的錯誤。 為了給不同錯誤類型放置不同的自定義錯誤頁面,子標簽被使用,那里不同的錯誤頁面基于錯誤的 status 代碼被指定。 為了實現頁面級別的錯誤解決,Page 指令能被修改為: “““ 因為 ASP.NET Debugging 是它內部一個重要的主題,因此我們將在下一章單獨地討論它。