Try and Catch continue running (after Exception)?

Asked

Viewed 3,052 times

5

How I put a block try and catch p/ continue the loop for, even if it does arrayindexofbounds and among other Exception? I put and it runs but the output of "m" does not print anything after!

for (String f : filename) {
    String temporario[] = f.split("_");
    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);
    }   
}
  • 2

    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...

  • 1

    You want to ignore files that don’t have one _, is that it? It’s more convenient - and more performatic - to simply test if ( 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).

  • 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.

  • 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?

2 answers

6

Place the block try/catch within the for:

for (String f : filename) {
    try {
        String temporario[] = f.split("_");
        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);
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        // aqui você faz o que quiser
    }
}

So the for will continue running even if one of your iterations results in an exception. However, as commented on in your question, you better clarify what you are trying to do to propose a better solution.

6

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.

  • In the part where you say exceptions should only be captured if it is inevitable and they should not be used for flow control, I believe that there are different "philosophical strands" about this. It is a fact that Runtime Exceptions should not be dealt with but exceptions that are more on the side of business rules are often used to make the code look like a description of the normal flow followed by the various exceptional flows, preventing the code from looking like a tangle of ifs. This can be seen in the Java Apis, as the file part.

  • Of course, just because it’s on JDK doesn’t mean it’s good design. Just exemplifying if I haven’t been clear.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.