E
emrefan
Does this program below indicate to you that there's a bug in java's do
loop. Or is it just one of those many traps that I've fallen into and
I've only got myself to blame? I was expecting the two loops would
behave much the same. Have I been bitten by the optimizer?
public class DoLoopBug {
public static void main( String[] args ) {
int retryCnt = 0;
doLoop1: do {
try {
if (retryCnt < 3)
throw new java.net.ConnectException();
break;
}
catch (Exception e) {
System.out.println( "caught exception; retryCnt=" +
retryCnt );
if (++retryCnt < 20)
continue doLoop1;
}
} while (false);
System.out.println( "after doLoop1, retryCnt = " + retryCnt +
"\n" );
retryCnt = 0;
doLoop2: do {
try {
if (retryCnt < 3)
throw new java.net.ConnectException();
break;
}
catch (Exception e) {
System.out.println( "caught exception; retryCnt=" +
retryCnt );
if (++retryCnt < 20)
continue doLoop2;
}
} while (true); // Here's difference from DoLoop1
System.out.println( "after doLoop2, retryCnt = " + retryCnt );
}
}
loop. Or is it just one of those many traps that I've fallen into and
I've only got myself to blame? I was expecting the two loops would
behave much the same. Have I been bitten by the optimizer?
public class DoLoopBug {
public static void main( String[] args ) {
int retryCnt = 0;
doLoop1: do {
try {
if (retryCnt < 3)
throw new java.net.ConnectException();
break;
}
catch (Exception e) {
System.out.println( "caught exception; retryCnt=" +
retryCnt );
if (++retryCnt < 20)
continue doLoop1;
}
} while (false);
System.out.println( "after doLoop1, retryCnt = " + retryCnt +
"\n" );
retryCnt = 0;
doLoop2: do {
try {
if (retryCnt < 3)
throw new java.net.ConnectException();
break;
}
catch (Exception e) {
System.out.println( "caught exception; retryCnt=" +
retryCnt );
if (++retryCnt < 20)
continue doLoop2;
}
} while (true); // Here's difference from DoLoop1
System.out.println( "after doLoop2, retryCnt = " + retryCnt );
}
}