2011-10-18 2 views
-1

표준에서 int를 읽으려고하면 컴파일 오류가 발생합니다.미해결 컴파일 : 처리되지 않은 예외 유형 IOException

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type IOException at SamplePackege.MainClass.main(MainClass.java:15) 

가 어떻게이 오류를 해결합니까 :

System.out.println("Hello Calculator : \n");   
int a=System.in.read(); 

이 프로그램은 예외를 throw?

내 코드 :

try { 
    Scanner sc = new Scanner(System.in); 
    int a=sc.nextInt(); 
} catch (Exception e) { 
    // TODO: handle exception 
} 
+3

좋은 자바 튜토리얼을 먼저 얻으십시오. –

+0

나는 더 나은 질문을하려고 노력하고 괜찮은 대답을 주었다. – ScArcher2

답변

2

in.read()는 유형 IOException이의 확인이 끝난 예외를 던질 수 있습니다.

당신은 당신은 IOException를 슬로우하는 프로그램을 변경할 수 있습니다 자바 Here.

에서 예외 처리에 대해 읽을 수 있습니다, 또는 당신은 시도의 catch 블록에서 읽기를 넣을 수 있습니다.

try{ 
    int a=System.in.read(); 
catch(IOException ioe){ 
    ioe.printStackTrace(); 
} 

또는

public static void main(String[] args) throws IOException { 
    System.out.println("Hello Calculator : \n"); 
    int a=System.in.read(); 
} 
1

이 프로그램은 버그가 없습니다.

read() 방법은 문제가 발생할 경우를 대비하여 Exception을 잡아야합니다.

try/catch 문 내부의 방법을 동봉 : 어떤 경우 에서

try { 
int a = System.in.read(); 
... 
} 
catch (Exception e) { 
e.printStackTrace(); 
} 

내가 강력하게 당신이이 일을 명확하게 언급되는 문서 및/또는 자바 튜토리얼를 사용하는 것이 좋습니다. 그 (것)들을 사용하여 밖으로 프로그램은 무의미하다. 당신은 많은 두통과 아마 우리 시대를 살릴 것입니다.

관련 문제