递归方法调用

jai*_*jai 1 java recursion

String url = getUrl();
try{
Connection con = getConnection(url, username, pwd);
}catch(ConnectionException e){
  cleanUpUrl(url);
  url = getUrl();
  con = getConnection(url, username, pwd);
}
Run Code Online (Sandbox Code Playgroud)

我要做上面的事情.如果我没有获得一个URL的连接,那么我将尝试使用另一个URL.同样有10URL我要一个接一个地尝试.

我将如何递归地编写方法?

getUrl()具有读取属性文件的逻辑,并为您提供10个随机URL. cleanUpUrl(url)与设置URL的到期时间有关,如果URL无效,将设置某些属性等.

编辑:对不起我想我错过了什么.递归,因为我已经做了方法调用,直到(我得到连接)或(所有的URL都无效并抛出了不同的异常).循环10次可能没有用,因为getUrl()的随机逻辑可能会多次选择相同的URL.

以下是否有意义?

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);
Run Code Online (Sandbox Code Playgroud)

当所有网址都无效时,getUrl()将抛出异常.

Tom*_*lak 6

递归?为什么?

如果你想连续尝试10件事,请使用for循环.