//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
}
最佳答案
我要做的主要改进是存储发生的所有异常,并以某种方式使它们在某个地方可用。否则,看起来还不错。
对于持怀疑态度的人来说,即使发生异常也想要完成某些工作并不是真正的异常。批处理是一个非常典型的案例。
关于java - 异常处理在错误时继续,然后重新抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7576744/