the best choice for try catch in Java
I have a vector which contains a list of files to open and parse, and I was wondering what is the best choice to do in my case.
This what I started to do :
for (int i =0 ; i< files.size() ; i++)
{
System.out.println("n°" + i + " : " + files.elementAt(i));
try
{
// open the files
}
catch (Exception e) {
// TODO: handle exception
}
}
or what do you thi开发者_开发知识库nk about this one?
try
{
for (int i =0 ; i< files.size() ; i++)
{
System.out.println("n°" + i + " : " + files.elementAt(i));
// open the files
}
}
catch (Exception e) {
// TODO: handle exception
}
EDIT:
My purpose is that, when I try to open a file that doesn't exist I must throw something or maybe write the exception in a log file and continue opening the other files.So I think the first solution is the best in my situation?
I would go with the first one - that way in case file 7 is corrupt for instance, you can still get the data from 8-10 (or whatever). And even if they are all corrupt, you don't lose anything.
I suppose it would come down to how your application should work. If one file being wrong will bring the whole thing to a screeching halt, the second is more appropriate, but generally I have found the first to be more useful.
I think the first is better, its advantage is that if there is an exception in one file, you still parse the others.
According to your edit - the second option will not fit, it will get out of the loop when an exception is thrown.
It's not the same !
In your first case, there are next iterations if you catch an Exception. In your second case, there are no next iterations if you catch an Exception !
That all depends of what you would like to achieve. If you'd like to try ALL files you should take the first option. If you'd like to stop in case of a single error than the seconds is better option.
The second one will avoid the overhead of entering and leaving the try/catch multiple times, but differs in behavior from the first. If you have an error with one file, the first case will allow the remaining files to still be attempted; in the second case, all further processing is ended.
Which method you use should be based entirely on which of these behaviors you want.
Careful: Those aren't equivalent! If one of the files fails, the first form (with try/catch in the loop body) will allow other files to be processed, while the second form will abandon the other files.
Focus on what functionality you need first.
After that... try/catch does incur a slight overhead, so the second form is a little quicker, but it comes with the cost of not being able to open the remaining files if an early one fails.
That depends entirely on what you want to do. The two alternatives behave completely differently when you fail on file #5 out of 10. In the first example it will continue with file 6, 7, 8...
In the second example it will only continue until a file fails, after which it gives up. If you know your desired behavior, you also know which one to choose.
Option 1:
This will try to open every file, even if one (or multiple) cause Exceptions.
Option 2:
This will stop attempting to open files if any single file causes an Exception.
If you can assume that if one file fails, all will fail - go with the first option, or if all files are necessary for correct operation - go with the first option, but if you want to load as many files as possible - go with the second option.
精彩评论