Logo         Community
The Company
Your Account
Contact Us
ASP.NET Basics (part 10): Making Exceptions
Learn all about ASP.NET exceptions and how to write code that traps and resolves them.

| Exceptionally Clever |

Let's look at the simple approach first. As explained earlier, this technique merely involves sending the user to a generic page when an exception occurs. There are two basic steps in this process: creating a page containing a generic error message, and modifying the server configuration so that the page is invoked when an exception occurs.

Let's illustrate this process with a simple example. Here's an ASP.NET script that divides a number by zero - a process guaranteed to make any programming language scream in anguish.

<script language="c#" runat="server">
void Page_Load() { 

int a = 19;
int b = 0;
int c = a/b;

<asp:label id="output" runat="server" />

Here's the output:

Output image

Ugly, huh?

Now, create the page that is shortly going to replace the gobbledygook above (call it "error.aspx"):

<% @ Page Language="C#" %>
<head><title>Error </title></head>
<asp:label id="errorMessage" runat="server" value="An error occurred. Please exit in single file, turning the light off after you leave." />

Next, you need to tell the Web server that it should load the page above whenever an exception occurs. This can be done by setting a value for the <customErrors> element in the "web.config" file. This file, usually located in the same directory as your ASP.NET scripts, allows you to configure some aspects of your application.

Here's what your "web.config" file should look like:

<customErrors defaultRedirect="error.aspx" mode="On" />

The "defaultRedirect" attribute of the <customErrors> element allows you to specify the error page to be displayed in the event of an exception. The "mode" attribute allows you to control the behaviour of the server in the event of an exception, and can take any of the following three values: "On", which will display the custom error page defined in the "defaultRedirect" attribute under all conditions; "Off", which will display the standard exception dump under all conditions; and "RemoteOnly", which will display the exception dump when the script containing the erroneous code is accessed from the local system itself, and the custom error page when it is accessed over the network or Internet. This allows a developer to debug errors without having to worry about their visual impact on end users, as they will only get to see the error page defined in the "defaultRedirect" attribute.

Now, if you retry the script above, you should see your custom error page instead of the standard exception page:

Output image

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback