我有以下 java 代码框架 -
try {
Question q = null; //List of questions. I have put 3 in the list, in my main function
int i = 0;
while (i <= questions.size()) {
System.out.println("iteration " + i);
q = questions.get(i);
try {
try {
System.out.println("Script completed");
break;
} catch (Exception e) {
// script is still executing... continue
}
//My entire logic is here.An exception is thrown in the first iteration
i++;
} catch (Exception e) {
//The thrown exception is caught here
try {
//The caught exception is handled here
} catch (IOException ioe) {
System.out.println("IO Exception..");
}
}
}
} catch (IOException ioe) {
System.out.println("No more communication due to the lack of data");
} catch (IllegalMonitorStateException imse) {
System.out.println("Illegal Monitor State Exception");
} catch (IllegalThreadStateException itse) {
System.out.println("Illegal Thread State Exception");
}
<小时/>
我得到的输出有点像这样 -
iteration 0
//Exception handling related output
iteration 0 //repeated because i++ doesn't happen since exception is thrown
//Execution takes place normally
iteration 1
//???????? - Here is where i am facing the problem. I am not getting
输出完全。我知道我仍然处于迭代1的原因(它与i++有关,由于第一次抛出异常,它不会发生一次)。但是有人可以帮助我如何成功执行此迭代吗?
最佳答案
您需要检查哪个 catch
block 在每次迭代中执行。如果 //try3
抛出异常或//try4
(或者如果根本没有抛出异常),则 i++
将被执行。
如果//try2
抛出异常(之前 //try3
或之后 //catch4
),然后 i++
不会被执行。
关于java - 异常处理冒险 III - 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5869957/