سؤال

I hava a method called test() that throws an exception.

I want to write a loop that executes as long as it throws an exception, and breaks when it no longer throws an exception.

Can any one hint me on the logic that must be use?

For example I tried,

int i=0;
while(test())
{
    i++;
}
System.out.println(i);
هل كانت مفيدة؟

المحلول

int i=0;
while (true) {
    try {
        test();
        break;
    } catch (Exception e) {
        i++; // Loop will continue
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top