When executing code:

for (int j = a ; j <= b; j++) { try { Document doc = Jsoup.connect("какой-то URL" + j).ignoreHttpErrors(true).timeout(1000 * 5).get(); } } 

Sometimes a SocketTimeoutException error pops up.

Can I process it in such a way that the cycle continues to work from the point of error? Is my implementation correct?

 catch (SocketTimeoutException ste) { System.out.println("Не удаётся подключиться. Пробуем снова"); j--; continue; } 

    1 answer 1

    Can I process it in such a way that the cycle continues to work from the point of error?

    Yes you can.

    Is my implementation correct?

    Yes, it is true.

    For verification, you can initially set a small timeout.

    For example, the result of executing this code here:

     for (int j=1; j<=10; j++) { try { System.out.println("Итерация #" + j); Document doc = Jsoup.connect("https://www.yandex.ru/").timeout(100).get(); System.out.println(" - Ок"); } catch (SocketTimeoutException ste) { System.out.println(" - Fail"); j--; continue; } } 

    I have this:

     Итерация #1 - Ок Итерация #2 - Fail Итерация #2 - Ок Итерация #3 - Ок Итерация #4 - Ок Итерация #5 - Fail Итерация #5 - Ок Итерация #6 - Ок Итерация #7 - Ок Итерация #8 - Fail Итерация #8 - Ок Итерация #9 - Ок Итерация #10 - Fail Итерация #10 - Fail Итерация #10 - Ок 

    Of course, you may have another one.

    • Thank. I note as correct after timeout - DriveMind Roman Nov
    • @ RomanDriveMind, For checking you can preset a very short timeout. - post_zeew
    • It worked. Thanks - Roman DriveMind