TwitterLinkedInBlog

Friday, February 20, 2009

SP2007 - Web Part HandleException Technique

Hide the exception message and stack trace in html comments and provides a Cancel link for the user to refresh the page:
/// <summary>
/// Clear all child controls and add an error message for display.
/// </summary>
private void HandleException(Exception ex)
{
m_ErrorHasOccurred = true;
Controls.Clear();
Controls.Add(new LiteralControl(string.Format("<!--{0}--><!--{1}-->{2}{3}",
ex.Message, ex.StackTrace,
"An error has occurred. Please contact your administrator.",
"<br/><a href='" + Page.Request.Url.ToString() + "'>Cancel</a>")));
}