How can I catch an exception in an enhanced for loop and restart the process for this loop?

断了今生、忘了曾经 提交于 2020-01-25 02:28:15

问题


I have started working with enhanced for loops due to it's best practices and habilities to work with ArrayLists.

My code basically gets an ArrayList that contains links to songs and parses this list downloading the songs. One of the exceptions thrown is the TimeoutException, whitch normally happens when the server is overloaded or there's an instability with the internet connection.

To clarify:

try
{
    for(Track track : album.getTracks())
    {
        songdown.downloadTrack(track, directorio, formataudio);
    }
}
catch (TimeoutException te)
{ 
    System.out.println("Timeout!");
}

track is an ArrayList whitch is parsed one by one by the songdown.downloadTrack function. When a download fails, a TimeoutException is raised, but I don't know how to treat this exception in order to delete the file I have generated and restart the for statement from the same track, so that the download can happen again.


回答1:


While it's an open question how advisable this would be, you'd be better off with an inner loop that retries until successful. Something like this:

for (Track track : album.getTracks()) {
    boolean downloaded = false;
    while (!downloaded) {
        try {
            songdown.downloadTrack(track, directorio, formataudio);
            downloaded = true;
        } catch (TimeoutException te) { /* delete partially downloaded song etc */ }
    }
}

You'd likely want a max # of retries within each iteration of the for loop or some other additional checks to avoid infinite looping.




回答2:


Please go through this first. First thing is that the catch block should start once the try block is done.

You can catch the exception in the catch block, and write the code which you want to get executed, when the exception occurs, which in you case is starting over again, and deleting the half downloaded songs.




回答3:


There are surely many valid approaches.

Here's one possibility:

int failureCount = 0;

while (failureCount < maxFailures) {
    try {
        parseAndDownload(someList);    
        break;
    }
    catch (Exception ex) {
        failureCount ++;
    }
}



回答4:


Here is a snippet example, I think you can solve your problem by following this way

ArrayList<String> a = new ArrayList<String>();
    a.add("a");
    a.add("w");
    a.add("d");
    //Iterator<String> i = a.iterator();
    for(String s:a){
        try{
            if(s.equals("w")){
                throw new Exception("Hello");
            }else{
                System.out.println(s);
            }
        }catch (Exception e) {
            continue;

        }
    }

In this snippet, I have explicitly throw an Exception, when it is thrown the catch block will execute, there i have used the keyword continue. This keyword is designed to tackle these kind of situation.



来源:https://stackoverflow.com/questions/9798518/how-can-i-catch-an-exception-in-an-enhanced-for-loop-and-restart-the-process-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!