开发者

Exception Handling Adventure III - Updated

开发者 https://www.devze.com 2023-03-02 03:39 出处:网络
I have this following java code skeleton - try { Question q = null; //List of questions. I have put 3 in the list, in my main function

I have this following java code skeleton -

        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"); 
               开发者_Python百科         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");
        }

And the output i get is somewhat like this -

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 

the output completely.I know the reason why i am still in iteration 1 (it has got to do with i++, which doesn't happen once due to the exception thrown for the first time). But can anybody help me how to successfully execute this iteration also?


You need to check which catch block gets executed in each iteration. If an exception is thrown in //try3 or //try4 (or if no exception is thrown at all), then i++ will be executed.

If an exception is thrown in //try2 (either before //try3 or after //catch4), then i++ will not be executed.

0

精彩评论

暂无评论...
验证码 换一张
取 消