我编写了一个类来加载我的应用程序的配置对象并跟踪它们,以便我可以通过单个方法调用轻松地写出更改或重新加载整个配置.但是,每个配置对象在执行IO时可能会抛出异常,但我不希望这些错误取消整个进程,以便让其他对象仍然有机会重新加载/写入.因此,我收集在迭代对象时抛出的所有异常,并将它们存储在循环之后抛出的超级异常中,因为必须仍然处理每个异常,并且必须通知某人确实出错了.但是,这种方法对我来说有点奇怪.有人用更清洁的解决方案吗?
以下是上述类的一些代码:
public synchronized void store() throws MultipleCauseException
{
MultipleCauseException me = new MultipleCauseException("unable to store some resources");
for(Resource resource : this.resources.values())
{
try
{
resource.store();
}
catch(StoreException e)
{
me.addCause(e);
}
}
if(me.hasCauses())
throw me;
}
Run Code Online (Sandbox Code Playgroud)