2011-05-03 4 views
0

업데이트 나는이 다음과 같은 자바 코드 골격이 -예외 -

 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에있는 이유를 알고있다. (처음으로 던진 예외 때문에 한번도 발생하지 않는 ++와 관련이있다.) 하지만 아무도 나를이 반복을 성공적으로 수행하는 방법을 도울 수 있습니까?

+0

여기에 정보가 충분하지 않습니다. –

+0

[SSCCE] (http://sscce.org/)를 제공하면 더 나은 답변을 얻을 수있을뿐만 아니라 사용자가 직접 작성하여 도움을 얻을 수도 있습니다. 앞으로도 그렇게 해보십시오! –

+0

세계 여러 곳에서 왜 예외를 잡을 필요가 있습니까? 이런 중첩 된 try 블록은 거대한 코드 냄새입니다. –

답변

4

각 반복마다catch 블록이 실행되는지 확인해야합니다. 을 확인해야합니다. //try3 또는 //try4에 예외가 발생하거나 예외가 전혀 발생하지 않으면 i++이 실행됩니다. 예외 (//catch4//try3 후 또는 그 이전 ) //try2 던져 경우

다음 i++ 실행될 수 없다.

+0

무엇이 hapening인지 압니다. 그러나 나는 그것을 극복하는 방법을 찾으려고합니다. 불행히도, 나는 성공적이지 않았습니다. – hari