.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Exception Handling in Asp.Net

Posted By:Sunil Yadav       Posted Date: July 30, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

This article explains the exception handling mechanism in Asp.Net

Exception Handling

Exception handling is built in mechanism in .Net Framework to handle the errors that occurred at run time. Proper exception/error mechanism should be in place to avoid information disclosure through error message. The information may contain Database, Web server, application path, Source file details etc. which an attacker can misuse to lunch an attack. It is important to log the error or exception occurred in the application on a different server.

In .Net Framework exception handling can be achieved using try-catch-finally block as shown below.


Public Page_Load(sender As Object, e As EventArgs)

            SqlConnection objSqlConnection;
            string sConn = "";
            sConn = ConfigurationManager.ConnectionStrings["DBConnectionString"].ToString();
            objSqlConnection = new SqlConnection(sConn);
            SqlCommand objSqlCommand = new SqlCommand();

            objSqlCommand.Connection = objSqlConnection;
            objSqlCommand.CommandType = CommandType.StoredProcedure;
            objSqlCommand.CommandText = "sp_getuser";
            objSqlCommand.Parameters.Add(new SqlParameter("@UserID", SqlDbType.VarChar)).Value = string.Empty;
            SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter(objSqlCommand);            
            DataSet objDataSet = new DataSet();
            DataTable myDataTable = objDataSet.Tables[0];

            catch (SqlException ex)

  // WriteLog writes exception occured on different server.
               if (objSqlConnection.State == ConnectionState.Open)


The exception can be handled both at page level (Page_Error), application level (Application_Error) and Web configuration level (Web.config). We can also create custom HTTP Module for exception handling.

1. Page_Error in .aspx page

 Page_Error event handler which is used to handle errors occurred at Page level. Following example shows how to handle errors at Page level.

 protected void Page_Error(object sender, EventArgs e)
            Exception objException = Server.GetLastError().GetBaseException();

2. Application_Error in Global.asax file

Application_Error event handler is used to handle error occurred at Application level.Following example shows how we can handle error occurred at application level.

   protected void Application_Error(object sender, EventArgs e)
            Exception objException = Server.GetLastError().GetBaseException();

NOTE: Do not user Server.ClearError() method to clear exception in Application_Error while you are redirecting user to custom error page using attribute as it clears all unhandled exception and redirection will fail.

3. in web.config file

The Custom Error tag in defined in the Web.config file can be used to handle errors. In web.config you can handle all errors at a single page or individually

Below is the sample to handle all error on one page.


 Separately handle each type of error as shown below.


4. Using custom HTTPModule

We can modularize an error handling mechanism by using HTTP Module. Follow the link below.


References :







 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend