Click here to Skip to main content
15,881,803 members
Articles / Web Development / IIS

Error Handling in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.56/5 (40 votes)
5 Jun 20059 min read 464.5K   5.2K   157  
Starts with user redirection settings with customErrors section; then moves to exception handling in different scopes. Explains how to prevent recursive loops, hande parser errors, base class error handling with internals as appropriate. Source code demonstrates the concepts illustrated.
using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;

namespace ErrorHandling 
{
	/// <summary>
	/// Summary description for Global.
	/// </summary>
	public class Global : System.Web.HttpApplication
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		public Global()
		{
			InitializeComponent();
		}	
		
		protected void Application_Start(Object sender, EventArgs e)
		{

		}
 
		protected void Session_Start(Object sender, EventArgs e)
		{

		}

		protected void Application_BeginRequest(Object sender, EventArgs e)
		{

		}

		protected void Application_EndRequest(Object sender, EventArgs e)
		{

		}

		protected void Application_AuthenticateRequest(Object sender, EventArgs e)
		{

		}

		protected void Application_Error(Object sender, EventArgs e)
		{
			// Uncomment the following line to demonstrate an infinite loop - caution: you will have 
			// to stop the request manually
//			Response.Redirect("NoSuchPageExists.aspx");

			
			// Uncomment the following code for handling application level errors - this would 
			// prevent customErrors settings to come into scope

//			string path;
//
//			path = "/ErrorHandling/ErrorPages/ApplicationError.html";
//			
//			// Uncomment this line to force exception for 404
////			path = "/ErrorHandling/ErrorPages/NoSuchPageExists.html";
//			
//			try
//			{
//				Server.ClearError();
//
//				// Attempt to redirect the user
//				Server.Transfer(path);
//			}
//			catch
//			{
//				// Error occured while redirecting user to an error page. We could do nothing
//				// now, but setting a status code and completing the request
//				Response.StatusCode = 404;
//				this.CompleteRequest();
//			}
		}

		protected void Session_End(Object sender, EventArgs e)
		{
		}

		protected void Application_End(Object sender, EventArgs e)
		{

		}
			
		#region Web Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.components = new System.ComponentModel.Container();
		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Rakesh Rajan is a Software Engineer from India working at Technopark, Trivandrum in Kerala. He is a Microsoft MVP and an MCSD (.NET) with a few other certifications, and had been working in .NET for the past 3 years. He graduated majoring in Computer Science during his memorable days at Ooty (a wonderful hill station in Southern India). You can find him posting at newgroups, writing articles, working on his own projects or taking some time off by listening to music by Enya or Yanni, or reading an Archer or Sheldon.

Find his online publications here.

Rakesh blogs at http://rakeshrajan.com/blog/ and maintains a site http://rakeshrajan.com/.
He used to blog at http://www.msmvps.com/rakeshrajan/.

Drop him a mail at rakeshrajan {at} mvps {dot} org.

Comments and Discussions