2013-09-10 3 views
0

다음 코드는 정적 변수 값이 설정된 문 앞에 줄을 인쇄 할 것으로 예상되지만 예상대로 작동하지 않습니다.자바 바이트 코드가 순차적으로 실행되지 않습니까?

+0

전체 스택 추적을 포함하고있는 문을 표시해주십시오'주()'예외가 발생되는 경우 (두 번째'at' 라인이어야 함). –

+0

@ mabbas, 그리고'hsbc.setIsInCrisis (true)'바로 위의 printf 문은 어떨까요? 출력은 다음과 같아야한다 : 현금 $ 100을 모으고, .... 예외가 잡혔다 .... 은행이 위기에있다 ... –

+0

java.lang.Exception : 은행이 위기에 처해있다 \t Bank.getCash (InstanceObjects.java : 13) \t (InstanceObjects.java:31) –

답변

5

문제 ...

import java.io.PrintWriter; 
class Bank{ 
private static boolean isInCrisis = false; 
public static boolean getIsInCrisis(){return Bank.isInCrisis;} 
public static boolean setIsInCrisis(boolean crisis){ 
    return Bank.isInCrisis = crisis; 
} 
public String getCash() throws Exception{ 
    if(!Bank.isInCrisis){ 
     return new String("$100"); 
    } 
    else{ 
     throw new Exception("Bank is in crisis"); 
    } 
} 
} 

public class InstanceObjects{ 
public static void main(String... st) { 
    try{ 
     Bank hsbc = new Bank(); 
     PrintWriter writer = new PrintWriter(System.out); 
     writer.printf("collect cash: %s\n",hsbc.getCash()); 
     writer.printf("collect cash: %s\n",hsbc.getCash()); 
     writer.printf("collect cash: %s\n",hsbc.getCash()); 
     writer.printf("collect cash: %s\n",hsbc.getCash()); 
     writer.printf("collect cash: %s\n",hsbc.getCash()); 
     writer.printf("collect cash: %s\n",hsbc.getCash()); 
     writer.printf("collect cash: %s\n",hsbc.getCash()); 
     hsbc.setIsInCrisis(true); 
     writer.printf("collect cash: %s\n",hsbc.getCash());    
     writer.close(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 
} 

출력은 예외 "은행이 위기에있다"를 던지고,하지만 첫 번째 메시지 "현금을 수집 ..."몇 줄 다음 throwed 예외 메시지를 인쇄한다 PrintWriter이 절대 플러시되지 않는다는 것입니다. 그것은 데이터를 누적하고 있지만 버퍼가 가득 차기 전에 예외가 발생하기 때문에 결코 콘솔에 쓰지 않습니다.

예외를 throw하는 호출 전에 writer.flush()을 호출하면 예상되는 메시지가 표시됩니다.

catchfinally 전에 실행 당신은 마침내이 닫혀 때 너무 작가를 플러시대로 데이터를 볼 수 있습니다 차단에 작가를 닫습니다 ...하지만 제외 후 합니다.

try (PrintWriter writer = new PrintWriter(System.out)) { 
    Bank hsbc = new Bank(); 
    ... 
} catch(Exception e){ 
    // writer will already be closed here 
    e.printStackTrace(); 
} 
+0

InstanceObjects.main에 있습니다! 감사, –

0

당신은

을 잊었 :

당신이 대신 try-with-resources 블록을 사용하는 경우 close가 "중첩"에서이 일이 있기 때문에, 당신은 제외하기 전에 데이터를 볼 수 있습니다 효과적으로/마지막 시도 받는 전송되지 않습니다

writer.flush();

printf 방법은, 당신의 메시지 버퍼에없는 자동 세척 앉아있다 않습니다 콘솔.

0

예외를 throw하는 메시지를 가져 오기 전에 스트림을 비워보십시오.

public class Main{ public static void main(String... st) { 
    try{ 
     Bank hsbc = new Bank(); 
     PrintWriter writer = new PrintWriter(System.out); 
     writer.printf("collect cash: %s\n",hsbc.getCash()); 
     writer.printf("collect cash: %s\n",hsbc.getCash()); 
     writer.printf("collect cash: %s\n",hsbc.getCash()); 
     writer.printf("collect cash: %s\n",hsbc.getCash()); 
     writer.printf("collect cash: %s\n",hsbc.getCash()); 
     writer.printf("collect cash: %s\n",hsbc.getCash()); 
     writer.printf("collect cash: %s\n",hsbc.getCash()); 
     writer.flush(); 
     hsbc.setIsInCrisis(true); 
     writer.printf("collect cash: %s\n",hsbc.getCash());    
     writer.close(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } } } 

인쇄 :

collect cash: $100 
collect cash: $100 
collect cash: $100 
collect cash: $100 
collect cash: $100 
collect cash: $100 
collect cash: $100 
java.lang.Exception: Bank is in crisis 
    at Bank.getCash(Main.java:13) 
    at Main.main(Main.java:32) 
관련 문제