ASP.NET
  Home arrow ASP.NET arrow Page 3 - ASP.NET Basics Part 10: Making Exceptions
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

ASP.NET Basics Part 10: Making Exceptions
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2004-01-19

    Table of Contents:
  • ASP.NET Basics Part 10: Making Exceptions
  • Word Games
  • Exceptionally Clever
  • A Custom Job
  • You Throw(), I'll Catch()
  • The More, the Merrier
  • Sending It to the Bitbucket
  • Rolling Your Own
  • Meeting the Family
  • All Wrapped Up
  • Digging Deeper
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    ASP.NET Basics Part 10: Making Exceptions - Exceptionally Clever


    (Page 3 of 12 )

    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
    () {   
    <
    P align=left
    <
    P align=left>int a 19;
    int b 
    0;
    int c 
    a/b;
    <
    P align=left
    <
    P align=left>} 

    </script>

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

    Here's the output:

    Ugly, huh?

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


    <% @ Page Language="C#" 
    %>
    <html>
    <head><title>Error 
    </title></head>
    <body>
    <asp:label id="errorMessage" 
    runat="server" 
    value
    ="An error occurred. Please exit in single file, 

    turning the light off after you leave." 
    />
    </body>
    </html>

    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:


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

    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:

    More ASP.NET Articles
    More By Harish Kamath (c) Melonfire


     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek