java - 异常处理冒险 III - 更新

标签 java exception try-catch

我有以下 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/

相关文章:

java - 从整数的 Iterable 中获取排序数组的最快方法

c# - 月日年错误

如果弹出空队列,C++ 抛出异常

java - Java读写文件和添加行号

mysql - 当指定的用户不存在时,PDO 连接不会返回错误

java - 如何在java中屏蔽颜色 channel ?

java - 检查 key 是否存在于 hashmap 中,其中 key 是嵌套的 hashmap

java - SVGPath 在按钮中无法正确调整大小

c++ - 如何将抛出的表达式从一个 catch block 转发到另一个?

c++ - 调用 MiniDumpWriteDump() 以捕获崩溃的最佳位置