I am using a .Net HtmlTextWriter
to generate HTML.
我正在使用.Net HtmlTextWriter生成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.
在这个例子中,如果在myObject.GenerateHtml()期间触发了错误异常,我将生成一个很好的错误html,但它前面会有一个永不关闭的开始span标记。
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.
现在我的跨度不会打开'直到我完成了艰苦的工作,但这对我来说看起来很尴尬。有没有办法用HtmlWriter回滚?即使我不得不放入使用块的负载。
I'm currently working in .Net 2.0, but a discussion of solutions in 3.5 would be ok.
我目前正在使用.Net 2.0,但讨论3.5中的解决方案是可以的。
2 个解决方案
#1
1
If you are only concerned about errors that occur during the GenerateHtml() call, and don't like the second approach (which seems fine to me), why not move the closing span tag into a finally block, and pull out the open call:
如果您只关心在GenerateHtml()调用期间发生的错误,并且不喜欢第二种方法(这对我来说似乎很好),为什么不将结束span标记移动到finally块中,并拉出打开的调用:
htw.RenderBeginTag( HtmlTextWriterTag.Span );
try
{
htw.Write(myObject.GenerateHtml());
}
catch (Exception e)
{
GenerateHtmlErrorMessage(htw);
}
finally
{
htw.RenderEndTag( );
}
This way the span is always opened and always closed. If GenerateHtml throws an exception, you catch it and generate the error inside the span, before closing it.
这样,跨度始终打开并始终关闭。如果GenerateHtml抛出异常,则在关闭它之前捕获它并在span内生成错误。
Of course, if the exception occurs trying to write the tags, then you are out of luck writing an error message anyway, so I'll assume that is being handled elsewhere.
当然,如果尝试编写标记时发生异常,那么无论如何你都不能写错误信息,所以我假设它正在其他地方处理。
#2
-2
You should avoid using try/catch, and instead check if the result is not what you expected. The only thing I can see here, is that myHTML can be null, so try something like this:
您应该避免使用try / catch,而是检查结果是否与预期不符。我在这里唯一能看到的是myHTML可以为null,所以尝试这样的事情:
string myHtml = myObject.GenerateHtml();
string myHtml = myObject.GenerateHtml();
if (myHTML != null)
{
htw.RenderBeginTag( HtmlTextWriterTag.Span );
htw.Write(myHtml)
htw.RenderEndTag( );
else
{
GenerateHtmlErrorMessage(htw);
}
#1
1
If you are only concerned about errors that occur during the GenerateHtml() call, and don't like the second approach (which seems fine to me), why not move the closing span tag into a finally block, and pull out the open call:
如果您只关心在GenerateHtml()调用期间发生的错误,并且不喜欢第二种方法(这对我来说似乎很好),为什么不将结束span标记移动到finally块中,并拉出打开的调用:
htw.RenderBeginTag( HtmlTextWriterTag.Span );
try
{
htw.Write(myObject.GenerateHtml());
}
catch (Exception e)
{
GenerateHtmlErrorMessage(htw);
}
finally
{
htw.RenderEndTag( );
}
This way the span is always opened and always closed. If GenerateHtml throws an exception, you catch it and generate the error inside the span, before closing it.
这样,跨度始终打开并始终关闭。如果GenerateHtml抛出异常,则在关闭它之前捕获它并在span内生成错误。
Of course, if the exception occurs trying to write the tags, then you are out of luck writing an error message anyway, so I'll assume that is being handled elsewhere.
当然,如果尝试编写标记时发生异常,那么无论如何你都不能写错误信息,所以我假设它正在其他地方处理。
#2
-2
You should avoid using try/catch, and instead check if the result is not what you expected. The only thing I can see here, is that myHTML can be null, so try something like this:
您应该避免使用try / catch,而是检查结果是否与预期不符。我在这里唯一能看到的是myHTML可以为null,所以尝试这样的事情:
string myHtml = myObject.GenerateHtml();
string myHtml = myObject.GenerateHtml();
if (myHTML != null)
{
htw.RenderBeginTag( HtmlTextWriterTag.Span );
htw.Write(myHtml)
htw.RenderEndTag( );
else
{
GenerateHtmlErrorMessage(htw);
}