public void demo() throws Exception { //TODO dosomething here throw new Exception(); }int count=0; public void run(){ try { demo(); } catch (Exception e) { count++; if (count < 5) { run(); } } }
或者你高兴,可以直接这样,连调用的方法都省去int count=0; public void demo(){ try { //TODO dosomething here //TODO It may throw an exception here } catch (Exception e) { count++; if (count < 5) { demo(); } } }
while( true )
{
try
{
new Demo();
break;
}catch(SocketException so)
{
}
}
int times = 0;
while( times<5 )
{
try
{
new Demo();
break;
}catch(SocketException so)
{
times++;
}
}
也就是想知道大家在写通信这块的时候一般是怎么处理这种问题的?我是想从别的服务器上请求数据。
public void demo() throws Exception {
//TODO dosomething here
throw new Exception();
}int count=0;
public void run(){
try {
demo();
} catch (Exception e) {
count++;
if (count < 5) {
run();
}
}
}
public void demo(){
try {
//TODO dosomething here
//TODO It may throw an exception here
} catch (Exception e) {
count++;
if (count < 5) {
demo();
}
}
}