First I must insist that you should not do this. It makes no sense to pass up a programming error. I talk about it a lot and you can start reading in that reply.
Second, it is not easy to help this way because we do not know what to do when the error occurs. You can even try to use your imagination but it’s still complicated because any solution could be wrong.
Finally, if there really is a reason to continue working despite having an invalid index, do not let the index be accessed, that is, prevent the error.
You should only capture exceptions that are unavoidable for one reason or another. Exceptions should not be used for normal flow control.
If an exception occurs and should not occur, it is programming error and it must be corrected. The most you should do in this case is let go back to the beginning and there catch an exception, write in a log that the error occurred for verification. It has several techniques to write on the logo, and can even alert the developer immediately in circumstances where external communication is working.
If the exception occurs and you know it is normal, we can get around the situation without a problem of race condition, then let it occur, check the situation before and avoid the error.
I will try here a solution of what I think is what you want:
for (String f : filename) {
String temporario[] = f.split("_");
if (temporario.length > 1) {
Date data = new Date(format.parse(temporario[1]).getTime());
if (m.containsKey(temporario[0])) {
if (m.get(temporario[0]).before(data)) {
m.put(temporario[0], data);
}
} else {
m.put(temporario[0], data);
}
}
}
I put in the Github for future reference.
Note that this solution does not seem robust to me. It will probably produce unwanted results if something is not compliant. But only you know whether this is acceptable or not.
Welcome to Stackoverflow! If you explain what you want with this, it would be easier for someone to help. What is the idea of letting a
arrayindexofbounds
, That would be dangerous at best...– Jorge B.
You want to ignore files that don’t have one
_
, is that it? It’s more convenient - and more performatic - to simply testif ( temporario.length < 2 ) continue;
instead of letting the exception happen and catch it later (even because, otherwise, it is difficult to differentiate between an exception occurring in your code or one occurring in the functions that your code calls - which could mask an unexpected error).– mgibsonbr
I don’t think that’s the case to close this question. It may be a little lacking in context but it was easy to answer. If he needs something more specific, he can improve, but the question is viable on its own. People who vote in a row need to read everything before voting.
– Maniero
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero