Try and Catch continue execution (after exception)?

4

How do I put a block try and catch to continue the loop for , even if it gives arrayindexofbounds and among others exception? I put it in 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);
    }   
}
    
asked by anonymous 27.01.2015 / 11:11

2 answers

6

First I must insist that you should not do this. It does not make sense to pass up a programming error. I talk a lot about it and you can start reading in that answer .

Second, it is not easy to help in 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 might be wrong.

Finally, if there is really any reason to continue working despite having an invalid index, do not let the index be accessed, that is, it prevents the error.

You should only catch 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 a programming error and it should be fixed. 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 for writing in 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, you can work around the problem without a race condition , then let it occur, check the situation before and avoid the error.

I'll try a solution here that 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);
        }
    }
}

Note that this solution does not seem robust to me. It will probably produce unwanted results if something is not in order. But only you know whether this is acceptable or not.

    
27.01.2015 / 11:58
4

Put block try / catch within 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 for will continue to run even if one of its iterations results in an exception. However, as you said in your question, it's good to clarify what you're trying to do to propose a better solution.

    
27.01.2015 / 11:53