views:

103

answers:

3
String url = getUrl();
try{
Connection con = getConnection(url, username, pwd);
}catch(ConnectionException e){
  cleanUpUrl(url);
  url = getUrl();
  con = getConnection(url, username, pwd);
}

I've to do something like above. if I don't get Connection with one URL then I'll be trying with another URL. Likewise there are 10URLs which I've to try one after the other.

How will I write the method recursively?

getUrl() has the logic to read the properties file and gives you random URL out of 10. cleanUpUrl(url) has something to do with the setting the expiry time of the URL, if the url is invalid, some property will be set etc etc.

EDIT: Sorry I think I missed something. Recursive because I've do make the method calls until (I get the connection) or (all the URLs are invalid and a different exception is thrown). Looping 10times might not help because the getUrl()'s random logic might pick the same URL more than once.

Does the following makes sense?

Connection con = null;
do{
 String url = getUrl();
 try{
  Connection con = getConnection(url, username, pwd);
 }catch(ConnectionException e){
  cleanUpUrl(url);
  continue;
 }catch(Exception e){
  return null;
 }
}while(con !=null);

getUrl() will throw exception when all urls are invalid.

+6  A: 

Recursively? Why?

If you want to try 10 things in a row, use a for loop.

Tomalak
Recursive because I've do make the method calls until (I get the connection) or (all the URLs are invalid and a different exception is thrown). Looping 10times might not help because the getUrl()'s random logic might pick the same URL more than once.
HanuAthena
@HanuAthena: Sorry, I don't get it. If `getUrl()` can pick the same URL more than once, why do you think this would be any different in a recursive approach? If you want to try something *until* a condition is fulfilled, use a `while` loop instead of a `for` loop. Still no need for recursion.
Tomalak
Also, if the 10 is not a hard-limit, why not loop till the url is non-null (on a side note, it is generally not a good idea to do this. It is better to have some sort of a configurable limit for the number of times you'd loop).
Gangadhar
+1: Recursion is *sometimes* an elegant model to use, but not in the cases where plain old loops are suitable. "When all you have is a hammer, everything looks like a nail"?
Andrzej Doyle
+1  A: 

I would put the "search for a proper url" in a method, loop though the code 10 times, and return the url you found suitable to break out of the loop. (Below the loop you could return null to indicate that none of the 10 urls was suitable.)

Something like

public String findGoodUrl() {
    for (int i = 0; i < 10; i++) {
        String url = getUrl();
        try{
            Connection con = getConnection(url, username, pwd);
            return url;
        } catch(ConnectionException e) {
            cleanUpUrl(url);
        }
    }
    return null;
}
aioobe
You might want to put the `return url;` inside the `try` block. ;-)
Tomalak
Good point ;) ...
aioobe
+1  A: 

What's the stop condition, a stack overflow?

Will it matter if you created a "while" loop?

LatinSuD