Search code examples
javaloopsexceptionlimit

Is there a limit for exceptions fireable at runtime in java?


I have a method, basically a loop (with all the proper catch conditions) where the exit condition is the frame being closed. This method that does something that needs an internet connection. If there isn't an internet connection it will recursively call itself until the internet connection is restored. I have noticed that after a certain amount of exceptions fired, it will simply stop to call recursively the method and therefore and no exceptions are fired after that. Is there a limit for exceptions fireable at runtime?

public Download()
{
    try {
            while(!frame.isWindowClosed())
            {
                //doSomething
            }
        } catch (FailingHttpStatusCodeException e) {
            e.printStackTrace();
            textArea.append("****** FailingHttpStatusCodeException ******\n");
            new Download();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            textArea.append("****** MalformedURLException ******\n");
            new Download();
        } catch (IOException e) {
            e.printStackTrace();
            textArea.append("****** IOException ******\n");
            new Download();
        } catch (Exception e) {
            e.printStackTrace();
            textArea.append("****** Exception ******\n");
            new Download();
        }
}

Solution

  • set the try inside the loop so as long as the frame is not closed, the loop will continue. If the catch block is the same for all your Exceptions you can just catch the highest Exception:

    public Download() {
        while (!frame.isWindowClosed()) {
            try {
                // doSomething
            } catch (Exception e) {
                e.printStackTrace();
                textArea.append("****** "+e.getClass().getName()+" ******\n");
            }
        }
    }
    

    As long as doSomething() did not succeeded in closing the frame the while loop will retry.