I am using a .Net HtmlTextWriter
to generate HTML.
try
{
htw.RenderBeginTag( HtmlTextWriterTag.Span );
htw.Write(myObject.GenerateHtml());
htw.RenderEndTag( );
}
catch (Exception e)
{
GenerateHtmlErrorMessage(htw);
}
In this example, if an error exception is fired during myObject.GenerateHtml()
, I will generate a nice error html but it will be preceded by an opening span
tag that is never closed.
I could refactor it like so
try
{
string myHtml = myObject.GenerateHtml();
// now hope we don't get any more exceptions
htw.RenderBeginTag( HtmlTextWriterTag.Span );
htw.Write(myHtml)
htw.RenderEndTag( );
}
catch (Exception e)
{
GenerateHtmlErrorMessage(htw);
}
Now my span doesn't open 'till I've done the hard work, but this just looks awkward to me. Is there any way do rollback with a HtmlWriter? Even if I had to put in loads of using blocks.
I'm currently working in .Net 2.0, but a discussion of solutions in 3.5 would be ok.