Page 124 - DCAP312_WEB_TECHNOLOGIES_II
P. 124
Web Technologies-II
Notes 1. When an error occurs
2. Where it occurred
3. What the error is
Having a central location such as the event log, database or some other log file to log errors is
essential for debugging this problem later.
IIS provides great error-handling capabilities. There are some problems with these though.
Sometimes we know an error will occur, and we cannot always trap for it in a nice way without
overriding the site’s default error redirection page. For example, upon access to a resource that
requires authentication, we may need to redirect to an application’s login page. Also, a very
common problem exists with Web hosts. If you have a hosted Web site, you usually have no
control over its IIS configuration. Thus, setting up custom error pages can be next to impossible
in traditional ASP. This is eliminated with ASP.NET, as you will learn as you read on.
6.4.2 The Solution
For such a list of problems, the solution is actually pretty simple. There are three places in ASP.
NET to define what happens to these unhandled errors.
1. In the web.config file’s customErrors section.
2. In the global.asax file’s Application_Error sub.
3. On the aspx or associated codebehind page in the Page_Error sub.
The actual order of error handling events is as follows:
1. On the Page itself, in the Page_Error sub (this is default, you can name it anything because
it specificed Handles MyBase.Error)
2. The global.asax Application_Error sub
3. The web.config file
When an exception occurs in your application, it should be an object inherited from type System.
Exception, and as such will have the following public members:
Table 6.2: An Object Inherited From Type System. Exception
HelpLink Gets or sets a link to the help file associated with this
exception.
InnerException Gets the Exception instance that caused the current
exception.
Message Gets a message that describes the current exception
Source Gets or sets the name of the application or the object
that causes the error.
StackTrace Gets a string representation of the frames on the call
stack at the time the current exception was thrown.
TargetSite Gets the method that throws the current exception.
6.4.3 Using the Page Error or on Error Sub
The first line of defense in error handling happens at the page level. You can override the MyBase.
Error sub as such: (Visual Studio will complete the code if you click either the Overrides or
Base Class events in the editor). The two functions you can use (one or the other, both will not
work as only one will get called)
118 LOVELY PROFESSIONAL UNIVERSITY