2012-09-21 3 views
0

또한 throws NumberFormatException, IOException은 무엇을 의미합니까? 나는 throws NumberFormatException, IOException가에 넣어하지 않으면 작동하지 않습니다java에서 throws 문은 무엇을 의미합니까?

BufferedReader nerd = new BufferedReader(new InputStreamReader(System.in)); 

하지만 BufferedReader을 말함으로써 BufferedReader를 사용하려고 계속.

+0

설명서를 확인 했습니까? – SLaks

+0

Google은 "java를 throw합니다." –

+2

http://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html –

답변

3

특정 방법으로 처리되지 않는 예외를 선언하는 데 사용되는 절을 예외와 인 이들을 명시 적으로 처리하거나 호출 계층 구조에서 다시 호출하도록 호출자에게 지시하십시오.

3

throws 키워드는 특정 메서드가 특정 예외를 "throw"할 수 있음을 나타냅니다. 가능한 경우 IOException (가능한 경우 다른 예외)을 try-catch 블록으로 처리하거나 메소드 선언에 throws IOException, (...)을 추가해야합니다. 다음과 같은 것 :

public void foo() throws IOException /* , AnotherException, ... */ { 
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    in.readLine(); 
    // etc. 
    in.close(); 
} 


public void foo() { 
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    try { 
     in.readLine(); 
     // etc. 
     in.close(); 
    } catch (IOException e) { 
     // handle the exception 
    } /* catch (AnotherException e1) {...} ... */ 
} 
1

throws 문은 함수가 오류를 throw 할 수 있음을 의미합니다. 즉, 현재 메서드를 끝내는 오류가 발생하여 스택에서 다음 'try catch'블록을 처리합니다. 당신이 추가 할 수 있습니다이 경우

는 메소드 선언 '.... 발생'또는 당신은 할 수 있습니다 :

try { 
    // code here 
} catch (Exception ex) { 
    // what to do on error here 
} 

읽기 http://docs.oracle.com/javase/tutorial/essential/exceptions/ 더 많은 정보를 원하시면.

관련 문제