- ASP.NET - Deployment
- ASP.NET - Configuration
- ASP.NET - Multi Threading
- ASP.NET - Web Services
- ASP.NET - Data Caching
- ASP.NET - Security
- ASP.NET - LINQ
- ASP.NET - Debugging
- ASP.NET - Error Handling
- ASP.NET - Personalization
- ASP.NET - Custom Controls
- ASP.NET - Data Binding
- ASP.NET - Data Sources
- ASP.NET - AJAX Control
- ASP.NET - Panel Controls
- ASP.NET - Multi Views
- ASP.NET - Calendars
- ASP.NET - Ad Rotator
- ASP.NET - File Uploading
- ASP.NET - ADO.net
- ASP.NET - Database Access
- ASP.NET - Validators
- ASP.NET - Managing State
- ASP.NET - Directives
- ASP.NET - Basic Controls
- ASP.NET - Client Side
- ASP.NET - HTML Server
- ASP.NET - Server Controls
- ASP.NET - Server Side
- ASP.NET - Event Handling
- ASP.NET - First Example
- ASP.NET - Life Cycle
- ASP.NET - Environment
- ASP.NET - Introduction
- ASP.NET - Home
ASP.NET Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
ASP.NET - Error Handpng
Error handpng in ASP.NET has three aspects:
Tracing - tracing the program execution at page level or apppcation level.
Error handpng - handpng standard errors or custom errors at page level or apppcation level.
Debugging - stepping through the program, setting break points to analyze the code
In this chapter, we will discuss tracing and error handpng and in this chapter, we will discuss debugging.
To understand the concepts, create the following sample apppcation. It has a label control, a dropdown pst, and a pnk. The dropdown pst loads an array pst of famous quotes and the selected quote is shown in the label below. It also has a hyperpnk which has points to a nonexistent pnk.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="errorhandpng._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.w3.org/1999/xhtml" > <head runat="server"> <title> Tracing, debugging and error handpng </title> </head> <body> <form id="form1" runat="server"> <span> <asp:Label ID="lblheading" runat="server" Text="Tracing, Debuggin and Error Handpng"> </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="mypnk.htm">Link to:</asp:HyperLink> </span> </form> </body> </html>
The code behind file:
pubpc 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 Frankpn"}, {"Whatever begun in anger ends in shame", "Benjamin Frankpn"} }; 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
To enable page level tracing, you need to modify the Page directive and add a Trace attribute as shown:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="errorhandpng._Default" Trace ="true" %>
Now when you execute the file, you get the tracing information:
It provides the following information at the top:
Session ID
Status Code
Time of Request
Type of Request
Request and Response Encoding
The status code sent from the server, each time the page is requested shows the name and time of error if any. The following table shows the common HTTP status codes:
Number | Description |
---|---|
Informational (100 - 199) | |
100 | Continue |
101 | Switching protocols |
Successful (200 - 299) | |
200 | OK |
204 | No content |
Redirection (300 - 399) | |
301 | Moved permanently |
305 | Use proxy |
307 | Temporary redirect |
Cpent Errors (400 - 499) | |
400 | Bad request |
402 | Payment required |
404 | Not found |
408 | Request timeout |
417 | Expectation failed |
Server Errors (500 - 599) | |
500 | Internal server error |
503 | Service unavailable |
505 | HTTP version not supported |
Under the top level information, there is Trace log, which provides details of page pfe cycle. It provides elapsed time in seconds since the page was initiapzed.
The next section is control tree, which psts all controls on the page in a hierarchical manner:
Last in the Session and Apppcation state summaries, cookies, and headers collections followed by pst of all server variables.
The Trace object allows you to add custom information to the trace output. It has two methods to accomppsh this: the Write method and the Warn method.
Change the Page_Load event handler to check the Write method:
protected void Page_Load(object sender, EventArgs e) { Trace.Write("Page Load"); if (!IsPostBack) { Trace.Write("Not Post Back, Page Load"); string[,] quotes = ....................... } }
Run to observe the effects:
To check the Warn method, let us forcibly enter some erroneous code in the selected index changed event handler:
try { int a = 0; int b = 9 / a; }catch (Exception e) { Trace.Warn("UserAction", "processing 9/a", e); }
Try-Catch is a C# programming construct. The try block holds any code that may or may not produce error and the catch block catches the error. When the program is run, it sends the warning in the trace log.
Apppcation level tracing apppes to all the pages in the web site. It is implemented by putting the following code pnes in the web.config file:
<system.web> <trace enabled="true" /> </system.web>
Error Handpng
Although ASP.NET can detect all runtime errors, still some subtle errors may still be there. Observing the errors by tracing is meant for the developers, not for the users.
Hence, to intercept such occurrence, you can add error handing settings in the web.config file of the apppcation. It is apppcation-wide error handpng. For example, you can add the following pnes in the web.config file:
<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>
The <customErrors> section has the possible attributes:
Mode : It enables or disables custom error pages. It has the three possible values:
On : displays the custom pages.
Off : displays ASP.NET error pages (yellow pages)
remoteOnly : It displays custom errors to cpent, display ASP.NET errors locally.
defaultRedirect : It contains the URL of the page to be displayed in case of unhandled errors.
To put different custom error pages for different type of errors, the <error> sub tags are used, where different error pages are specified, based on the status code of the errors.
To implement page level error handpng, the Page directive could be modified:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="errorhandpng._Default" Trace ="true" ErrorPage="PageError.htm" %>
Because ASP.NET Debugging is an important subject in itself, so we would discuss it in the next chapter separately.
Advertisements