【发布时间】:2011-09-27 23:23:30
【问题描述】:
//Assume list1 and list2 are populated with states as 2
foo (List<Class1> list1, List <Class1> list2) {
boolean error = false;
try {
operate on list1
} catch (Exception e) {
error = true;
//Modify list1 objects to state 1
}
try {
operate on list2
} catch (Exception e) {
error = true;
//Modify list2 objects to state 1
}
//What are the problems and what is the best practice for this
if (error)
throw new Exception(…); //How should i really rethrow the exception
}
【问题讨论】:
-
这样的设计合理吗?如果第一个操作已经发生错误并且您只会抛出异常,那么对 list2 进行操作有什么意义?为什么不马上扔掉?