try {
methodA();
} catch (Exception e) {
}
这个try catch块放在main()函数中,一旦methodA()抛出了异常,程序就会终止,我希望当程序抛出异常时候,程序能够自动再去执行一遍methodA(),直到methodA()正常结束为止。请问有什么好办法吗?
methodA();
} catch (Exception e) {
}
这个try catch块放在main()函数中,一旦methodA()抛出了异常,程序就会终止,我希望当程序抛出异常时候,程序能够自动再去执行一遍methodA(),直到methodA()正常结束为止。请问有什么好办法吗?
boolean b = true;
while(b) {
try {
methodA();
b = false;
} catch (Exception e) { continue;
}
}不知道这样行不行
public class Test {
public int randomMethod() throws Exception{
Random r = new Random();
int i = r.nextInt(10);
if(i<=5) // 不大于5就抛异常
throw new Exception("随机数小了点!" + i);
return i;
} public void doSomething() {
int i = 0;
while (true) {
try {
i = randomMethod();
break;
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
System.out.println("i = " + i);
} public static void main(String[] args) throws Exception {
new Test().doSomething();
}
}
try {
methodA();
}catch(Exception e) {
methodB()
}
}