I have a program which basically manages batch runs of some underlying Fortran programs, and at the end, I want to move all the related files into a different directory.
At the moment, there is something along the lines of this:
private void finalClean() throws Exception{
Script.move(file1, calcDir);
Script.move(file2, calcDir);
Script.move(file3, calcDir);
...
}
Now, the nature of these jobs is that they do not always run to completion, but the finalClean
method is in the finally clause, so it is always run. The problem is, that depending on where the run fails, not all of the files are present, which means that if one of those files isn't present, it throws an exception, causing none of the remaining files to be moved. The files are not always created in the same order either, which means I can't just order them as they would be created and then not have to worry about remaining files, as they're not created.
My current solution to this is:
ArrayList<String> files_to_move = new ArrayList<String>();
files_to_move.add("file_name_1");
files_to_move.add("file_name_1");
files_to_move.add("file_name_1");
...
for(String file_name : files_to_move){
try {
Script.move(QNFiles.getProject(), file_name, calcDir);
} catch (Exception ex) {
Logger...blah.blah.blah;
}
}
But I'm not overly pleased with how it looks. Is there a way to have a some kind of construct around the whole loop which just grabs all the exceptions, ignores them, and then throws an exception containing info on all of the others afterwards? Maybe a way I can temporarily ignore a subset of exceptions and deal with them all later, without having the try catch in the loop?
I don't use Java as much as Python anymore, so I suspect my wanting to neaten this up is a symptom of becoming used to that, but still: is there a better way to do this?