可以说你有一些资源清理如下:这是C#。finally块中的多项式操作
try{/*stuff*/}
catch(Exception e) {/*rollback logs etc.*/}
finally{
if(context.Transaction != null)
context.Transaction.Dispose();
context.Connection.Close();
context.Connection.Dispose();
}
这样做会更健壮吗?
try{/*stuff*/}
catch(Exception e) {/*rollback logs etc.*/}
finally{
try{
if(context.Transaction != null)
context.Transaction.Dispose();
}catch(Exception e){/*logs*/}
finally{
context.Connection.Close();
context.Connection.Dispose();
}
}
这样,如果transaction.dispose管理失败,连接将被给予关闭的机会。
通过这种逻辑,你可以将嵌套try-catch结构的概念应用到无限级别。它仍然听起来像健壮? – Cerebrus 2009-02-21 15:40:46
@Cerberus:只有你有无限的资源,才会遇到这种情况? – 2009-02-21 16:30:58