简体   繁体   中英

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();
        }
}

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.

I think it's better for you to have that loop inside a method that is not inside the constructor.. Then call the method from the constructor.

I think what you should be doing is having a mechanism to check if there is network connectivity.. Then perform the required operation if there is connection. If there is no internet connectivity, then continue. You'll have to wrap this inside a while loop of course

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM