0
我有一个xmlbuilder工具类调用几个方法来建立一个XML文件如何处理异常正确
public XMLBuilder(String searchVal)
{
this.searchVal = searchVal;
try
{
getData();
returnedData = processDataInToOriginalFormat();
WriteBasicTemplate();
}
catch (WebException)
{
//If this is thrown then there was an error processing the HTTP request for MSO data.
//In this case then i should avoid writing the xml for concordance.
serviceAvailable = false;
MessageBox.Show("Could not connect to the required Service.");
}
catch (NoDataFoundException ndfe)
{
//propegate this back up the chain to the calling class
throw;
}
processDataInToOriginalFormat();
这是导致异常的类的方法,如果服务不可用的,我已经传播了异常回到这里来处理。我将尝试设置一个布尔标志来指示是否写入一定数量的xml。如果该标志为假,则不写。
但我忘了,异常停止程序流,现在我意识到这是不可能的,就像发生异常,其余代码不会恢复。我怎样才能解决这个问题?只需将WriteBasicTemplate();
调用添加到我的catch子句中?
感谢
当然要将异常传播到更适合处理它的地方,您需要重新抛出? finally语句解决了我所有的问题,并且很抱歉让逻辑难以遵循,我必须删除业务数据。 – ricki 2011-04-20 12:04:31
如果你完全没有理解它,你不需要重新抛出它。 ;-)但是有时候人们必须抓住例外来决定是否可以处理。然后你必须重新抛出它当然。这是我试图澄清的一点。 – Achim 2011-04-20 12:11:18