user1282468
user1282468

Reputation:

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

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.

Upvotes: 1

Views: 2037

Answers (4)

Chandra Sekhar
Chandra Sekhar

Reputation: 19500

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.

Upvotes: 0

jahroy
jahroy

Reputation: 22692

There are surely many valid approaches.

Here's one possibility:

int failureCount = 0;

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

Upvotes: 1

Anuj Balan
Anuj Balan

Reputation: 7729

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.

Upvotes: 0

Paul Karlin
Paul Karlin

Reputation: 840

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.

Upvotes: 6

Related Questions