AAron nAAs

All MindsharpBlogs

AAron's SharePoint notepad

My Links

Archives

Blog Stats

My Sites

Debugging tips for SharePoint

TheKid has some good “Debugging tips for SharePoint.“ There are worthy suggestions in his articles some well known, some scarey (such as debugging with WinDbg).

http://blog.thekid.me.uk/archive/2007/07/25/debugging-tips-for-sharepoint-and-wss-exceptions.aspx

I wanted to point out that the best way I've found to control a Render() method from destroynig a page is not only to throw a try catch around it, but also to buffer the web part's output. If the web part render code succeeds, then output the buffer. If the web part render failed for some reason, it could leave the page in disarray with unclosed html tags, partial javascript... many things could destroy the page's usability or cause subtle bugs. The all-or-nothing approach helps clear that up.

Consider this enhancement to Render():

protected override void Render(HtmlTextWriter liveWriter)
{
  StringBuilder buffer = new StringBuilder();
  StringWriter writer = new StringWriter(buffer);
  try
  {
    // Do something to render the control, which may cause an exception

    int dinos = 0;
    int people = int.MaxValue;
    writer.WriteLine(
"Everybody kill the dinosaur");
    writer.WriteLine(
"<table><tr><td>");
    writer.WriteLine(
"ratio: " + people / dinos); // This will blowup leaving partial table in buffer.
    writer.WriteLine("</td></tr><table>");
  }
  catch (Exception
ex)
  {
    Trace
.Write(ex);
    Trace.WriteLine(HttpContext
.Current.Request.Url.ToString());
    // Maybe render an error message
    liveWriter.WriteLine("An error occurred, deal with it."
);
    return
;
  }
  // Output buffer since no exception occurred.
  writer.Close();
  liveWriter.Write(buffer.ToString());
}

posted on Friday, September 14, 2007 10:18 PM

Feedback

No comments posted yet.
Title  
Name  
Url
CAPTCHA
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments