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.
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:
You’d likely want a max # of retries within each iteration of the for loop or some other additional checks to avoid infinite looping.