Exceptional Handling in Asp.net:

Exceptional Handling in Asp.net can be done in three ways 1.using Try Catch blocks. 2.Using Error handlers 3.using Ihttp Modules I will explain herethe third type i.e using http modules.

Purpose or Error Handling ?
       Redirecting the remote user to a custom error page which shows the basic details of the Error and what need to be done in the next step.This is simply show a understandable message to the User.
    Avoid displaying database related errors : Database related exceptions includes sensitive details like database server,database name,table,stored procedure etc. So that an attacker can use.It is advisable to log the exceptions and display a generic error message.
What is need of handling errors in the Http modules?
1.You can maintain all your login at a single place to handle errors .
2.As it is a Class library it creates a DLL , all you need to do is add the dll to your application bin folder , and register it in web.config(i will show to register it in web.config.)
3.Maintaining would be Quite easy
Example showing the Error Handling Using Http Module.
  First Step is to Create a New Project and Select Class Library and implemt like shown below...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Globalization;

namespace ErrorHandlingClassLib
//the Class is implementing the IHttpModule Interface.
public class ErrorModule:IHttpModule

#region IHttpModule Members      

public void Init(HttpApplication context)
//Raising the Event for handling the Error using application_Error Event
            context.Error +=
new EventHandler(application_Error);

void application_Error(object sender, EventArgs e)
            HttpContext hContext = HttpContext.
//Getting the last occured error for the current
            Exception excp = hContext.Server.GetLastError();
//specfying a path for the Error to log
            string path =
"~/ErrorLog/" + DateTime.Today.ToString("dd-MMM-yy") + ".txt";
//Checking if the File Exists or Not.. if file Doesnt Exist then creating the file in that location.
if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
            StreamWriter objStreamWriter = File.AppendText(System.Web.HttpContext.
//appending each line of text to the file .
WriteLine("\r \n ErrorLog Entry");
WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));
            string Error =
"Error in File Path : " + System.Web.HttpContext.Current.Request.Url.ToString() + "\n Error Message : " + excp.Message;
WriteLine("************ -------------------- ****************");
//closing the stream........

public void Dispose()
new NotImplementedException();


After creating the module, then the final step is to register this in the Web.Config of the Application for which you are using this ,you need to register under Http modules tag

      <add name="ErrorModuleClass" type="ErrorModule1.ErrorModuleClass "/>

Thus the application errror handling can be managed using Http modules .

By TSN ...   Popularity  (2052 Views)