Click here to Skip to main content
15,867,771 members
Articles / Web Development / ASP.NET

Handling Error Centrally in a SharePoint Application

Rate me:
Please Sign up or sign in to vote.
4.21/5 (11 votes)
20 May 2008CPOL4 min read 84.7K   596   22  
An article on handling errors centrally in a SharePoint application, using IHttpModule.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MyErrorHandler : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            if (HttpContext.Current.Application.Get("LastException") != null)
            {
                try
                {


                    Exception ex = (Exception)HttpContext.Current.Application.Get("LastException");
                    string Code = (string)HttpContext.Current.Application.Get("ErrorCode");
                    uxErrorMessage.Text = ex.Message;
                    uxStackTrace.Text = ex.StackTrace;
                    uxErrorCode.Text = Code;
                }
                catch (Exception exc)
                {
                    uxErrorMessage.Text = exc.Message;
                    uxStackTrace.Text = exc.StackTrace;
                    uxErrorCode.Text = "Unexpected Error";
                }
            }
            else
            {

                uxErrorMessage.Text = "Does not contain any information about error!!!";
                uxStackTrace.Text = "Not available!";
                uxErrorCode.Text = "Not available!";
            }

            //HttpContext.Current.Application.Remove("LastException");
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Gen-i, Australia
Australia Australia
Expertise area: ASP.NET, C#, Microsoft Office SharePoint 2010 & 2007, Web Service, Windows-based Applications etc.

Blog: http://mydevdiary.blogspot.com/

Comments and Discussions