SharePoint generic handle web part error

If you have web part that has code behind to do something (like add data to a list) and it has an exception during processing you need a way to display that error.
Here is a generic method I use to display the error.
I put this in all my catches.  It will add a new literal control with the error.

        /// <summary>
       /// Clear all child controls and add an error message for display.
       /// </summary>
       /// <param name="ex"></param>
       private void HandleException(Exception ex)
           this._error = true;
           this.Controls.Add(new LiteralControl(ex.Message));
[tweetmeme only_single=”false”]

One thought on “SharePoint generic handle web part error

  1. Hello,
    Nice trick right there. I remember when I first started in SharePoint. I did get a lot of errors on WebPart which leads me to either WebPart Maintenance Page or just complete Correlation ID error. BTW, do you think of creating a virtual webpart class that contains all the default handling like this and go from there ? May be these handlers could be virtual protected so if we need special exception handling for a certain webpart we can just override the base method ?
    I’m not trying to be smart ; ) but it is just my thought.

Got something to add?

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s